我知道此网站已提供相关问题。我试过了,但没有一个没有提供我问题的确切答案。我正在构建一个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类中捕获传递的参数(“test”)并将其分配给变量?
这是我的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
答案 0 :(得分:0)
您可以通过 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 */
}
}