C# 从c类重定向到带有参数的xaml页面,并在xaml页面中捕获它们

C# 从c类重定向到带有参数的xaml页面,并在xaml页面中捕获它们,c#,wpf,xaml,C#,Wpf,Xaml,我知道这个网站上已经有相关的问题了。我试过了,但没有一个不能准确回答我的问题。我正在构建一个WPF应用程序,我有一个C类,它调用要加载的XAML页面。这是我的源页面: public class ChartController : INotifyPropertyChanged { public ObservableCollection<string> ChartTypes { get; set; } public ChartController() {

我知道这个网站上已经有相关的问题了。我试过了,但没有一个不能准确回答我的问题。我正在构建一个WPF应用程序,我有一个C类,它调用要加载的XAML页面。这是我的源页面:

 public class ChartController : INotifyPropertyChanged
{
    public ObservableCollection<string> ChartTypes { get; set; }

    public ChartController()
    {
        ChartTypes = new ObservableCollection<string>();
        ChartTypes.Add("Pie");
        ChartTypes.Add("Doughnut");
        ChartTypes.Add("Clustered Bar");
        ChartTypes.Add("Clustered Column");
        ChartTypes.Add("Stacked Bar");
        ChartTypes.Add("Stacked Column");
        ChartTypes.Add("Stacked Bar Percentage");
        ChartTypes.Add("Stacked Column Percentage");
    }

    private string _simpleStringProperty;
    public string SimpleStringProperty
    {
        get { return _simpleStringProperty; }
        set
        {
            _simpleStringProperty = value;
            if (value.Equals("Pie"))
            {
                SelectedPageChart = new Uri("..\\Graphs\\GraphTemplates\\PieChart.xaml?parameter=test", UriKind.Relative);
            }
            if (value.Equals("Doughnut"))
            {
                SelectedPageChart = new Uri("..\\Graphs\\GraphTemplates\\DoughnutChart.xaml", UriKind.Relative);
            }
            if (value.Equals("Clustered Column"))
            {
                SelectedPageChart = new Uri("..\\Graphs\\GraphTemplates\\ClusteredColumnChart.xaml", UriKind.Relative);
            }
            if (value.Equals("Clustered Bar"))
            {
                SelectedPageChart = new Uri("..\\Graphs\\GraphTemplates\\ClusteredBarChart.xaml", UriKind.Relative);
            }
            if (value.Equals("Stacked Bar"))
            {
                SelectedPageChart = new Uri("..\\Graphs\\GraphTemplates\\StackedBarChart.xaml", UriKind.Relative);
            }
            if (value.Equals("Stacked Column"))
            {
                SelectedPageChart = new Uri("..\\Graphs\\GraphTemplates\\StackedColumnChart.xaml", UriKind.Relative);
            }
            if (value.Equals("Stacked Bar Percentage"))
            {
                SelectedPageChart = new Uri("..\\Graphs\\GraphTemplates\\StackedBarChart100Percent.xaml", UriKind.Relative);
            }
            if (value.Equals("Stacked Column Percentage"))
            {
                SelectedPageChart = new Uri("..\\Graphs\\GraphTemplates\\StackedColumnChart100Percent.xaml", UriKind.Relative);
            }
            if (value.Equals("Radial Gauge"))
            {
                SelectedPageChart = new Uri("..\\Graphs\\GraphTemplates\\RadialGaugeChart.xaml", UriKind.Relative);
            }
            OnPropertyChanged("SimpleStringProperty");

        }
    }

    private Uri _selectedPageChart;
    public Uri SelectedPageChart
    {
        get { return _selectedPageChart; }
        set
        {
            _selectedPageChart = value;
            OnPropertyChanged("SelectedPageChart");
        }
    }
    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}
我的问题是如何在PieChart.xaml.cs类中捕获通过的参数测试并将其分配给变量

这是我的piechart.xaml

 <Page x:Class="ModernUIForWPFSample.WithoutBackButton.Graphs.GraphTemplates.Page1"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
  xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
  xmlns:metroChart="clr-namespace:De.TorstenMandelkow.MetroChart;assembly=De.TorstenMandelkow.MetroChart" 
  mc:Ignorable="d" 
  d:DesignHeight="300" d:DesignWidth="300"
Title="Page1">

<Grid>

    <metroChart:PieChart x:Name="pieChart"
Style="{StaticResource MinimalChartStyle}"
ChartTitle="Minimal Pie Chart"
SelectedItem="{Binding Path=SelectedItem, Mode=TwoWay}" >
        <metroChart:PieChart.Series>
            <metroChart:ChartSeries x:Name="pieChartSeries"
        SeriesTitle="Errors"
        DisplayMember="Year"
        ValueMember="Cost"
        ItemsSource="{Binding Path=Errors}" />
        </metroChart:PieChart.Series>
    </metroChart:PieChart>

</Grid>

我正在使用Visual Studio 2013 DotNet framework 4.5和WPF,您可以通过NavigationService.CurrentSource(返回Uri对象)提取参数。但更好的方法是创建特定页面的实例,并使用NavigationService.Navigate方法的重载,该方法以对象作为参数

private Page _selectedPage;
private string _simpleString;

public string SimpleString
{
   get { /* ... */ }
   set 
   {
        _simpleStringProperty = value;
        if (value.Equals("Pie"))
        {
            SelectedPage = new PieChart("test");
        }
        /* rest of the setter */
   }
}

看看目的地页面中的@Lukas,我尝试了您提供的链接中所述的第一种方法。但我在NavigationContext中遇到了一个错误。它表示当前上下文中不存在navigationContext请提供更多行code@LukasKubis这个问题被编辑成更多的细节。如果您在回答之前需要澄清什么,请放心,您使用的是哪种应用程序?WPF应用程序或WPF浏览器应用程序当我使用导航服务时。。。它表示导航服务在当前上下文中不可用。!我缺少什么?您在哪里使用NavigationService?在目标页面上,Page或NavigationWindow类包含NavigationService属性