提前道歉,我只有不到一个月的XAML和WPF经验,并且一直在尽可能多地搜索,但是我们开始了。
我有一个窗口,里面有一个框架,我一直在使用页面来交换框架内的内容。我一直是这样做的,例如:
private void NextPageButtonPressed(object sender, RoutedEventArgs e)
{
this.NavigationService.Navigate(new Page2())
}
在没有传递数据的情况下,这对于导航很有效,但是当我尝试从page2到page3并尝试从各种复选框和组合框中传递数据时,如下所示:
private void GoToNextPage(object sender, RoutedEventArgs e)
{
object[] isKaliAndDistroSelected = new object[] {GetIsKali(), GetSelectedDistro()};
Page3 pg3 = new Page3();
this.NavigationService.LoadCompleted += pg3.NavigationService_LoadCompleted;
this.NavigationService.Navigate(pg3, isKaliAndDistroSelected);
}
与以下代码在第3页:
private string distro="";
private bool isKali = false;
public Page3()
{
InitializeComponet();
RunOnDoneLoadingPage(distro,isKali);
}
public void NavigationService_LoadCompleted(object sender, NavigationEventArgs e)
{
var test = (object[]) e.ExtraData;
isKali = (bool) test[0];
distro = (string) test[1];
NavigationService.LoadCompleted -= NavigationService_LoadCompleted;
}
它最终不更新值isKali或distro,除非我将RunOnDoneLoadingPage(distro,isKali);
放在Page3的NavigationService_LoadCompleted
函数的末尾。我不想这样做的原因是因为我希望页面加载,然后运行RunOnDoneLoadingPage()
,因为该函数操纵加载/进度条。当我把它放在NavigationService_LoadCompleted
时,page2总是冻结几次,然后显示Page3的加载/进度为100%,使加载页面基本上无用。
所以我的问题是我如何在2页之间传递数据,并让下一页显示自己,但不开始一个方法,直到前一页的数据已经传递
提前谢谢你,就像我说的,我对XAML工作相当陌生,所以如果我能提供更多,请告诉我
MVVM可以帮助您。
这是一个简单的视图模型:
public class MyVM : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private string propAOfPage1;
public string PropAOfPage1
{
get => propAOfPage1;
set
{
if (value != this.propAOfPage1)
{
propAOfPage1= value;
NotifyPropertyChanged();
}
}
}
private string propBOfPage1;
public string PropBOfPage1
{
get => propBOfPage1;
set
{
if (value != this.propBOfPage1)
{
propBOfPage1= value;
NotifyPropertyChanged();
}
}
}
private string propCOfPage2;
public string PropCOfPage2
{
get => propCOfPage2;
set
{
if (value != this.propCOfPage2)
{
propCOfPage2= value;
NotifyPropertyChanged();
}
}
}
private string propDOfPage2;
public string PropDOfPage2
{
get => propDOfPage2;
set
{
if (value != this.propDOfPage2)
{
propDOfPage2= value;
NotifyPropertyChanged();
}
}
}
}
,这里是一个简单的视图:
<!-- Page1 -->
<StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Prop A:" />
<TextBox Text="{Binding PropAOfPage1}" Width="100" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Prop B:" />
<TextBox Text="{Binding PropBOfPage1}" Width="100" />
</StackPanel>
</StackPanel>
<!-- Page2 -->
<StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Prop C:" />
<TextBox Text="{Binding PropCOfPage2}" Width="100" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Prop D:" />
<TextBox Text="{Binding PropDOfPage2}" Width="100" />
</StackPanel>
</StackPanel>
将Frame.DataContext
设置为MyVM
对象,那么这个MyVM
对象将在您的页面之间共享。您可以自由导航,无需考虑在每个页面中获取或设置控件属性。
扩展阅读:https://www.google.com/search?q=wpf+frame+page+mvvm