【发布时间】:2013-10-14 17:54:35
【问题描述】:
我在更新我正在使用的 windows phone 7 应用程序的界面时遇到问题。
我有一个MainPage.xaml 和它的MainViewModel.cs,它们处理子视图用户控件ChildView1.xaml 如何出现在MainPage 中。 ChildView1 也有它的 ViewModel,它是Child1ViewModel.cs。基本上 myToggleButton 会从 MainPage 触发,这将触发 DataStateBehavior 进入显示或隐藏状态以分别显示或隐藏 ChildView1.xaml。 DataStateBehavior 绑定到布尔值CanShowView,而myToggleButton 绑定到名为ButtonChecked 的Icommand,它具有中继函数SwitchVisibility,它将反转CanShowView 布尔值以显示或隐藏视图。多亏了我使用的 MVVM 灯的RaisePropertyChanged,一切运行良好。
但是,ChildView1.xaml 有一个 myCloseButton,它应该从 MainViewModel 调用 SwitchVisibility 函数,这反过来又会恢复 CanShowView 中的 RaisePropertyChanged 和 CanShowView 中的 RaisePropertyChanged 应该触发以拥有 @987654341 @close,但问题是视图保持可见并且由于某种原因永远不会关闭!我通过调试器注意到CanShowView 中的RaisePropertyChanged 似乎没有触发。我想这就是问题所在。我做错了什么?我怎样才能强迫它开火?我使用了多个RaisePropertyChanged,但即使调试器显示我的函数中的值确实发生了变化,似乎也没有触发。
这是我的代码。 MainPage.xaml:
<phone:PhoneApplicationPage
...
>
<phone:PhoneApplicationPage.DataContext>
<Binding Path="Main" Source="{StaticResource Locator}"/>
</phone:PhoneApplicationPage.DataContext>
...
<Grid x:Name="LayoutRoot">
...
<ToggleButton x:Name="myToggleButton" Command="{Binding Main.ButtonChecked, Source={StaticResource Locator}}" >
<local:ChildView1 x:Name="myChildView1" Grid.Row="0" RenderTransformOrigin="0.5,0.5">
<i:Interaction.Behaviors>
<ec:DataStateBehavior Binding="{Binding Main.CanShowView, Source={StaticResource Locator}}" Value="True" TrueState="showChildView1" FalseState="hideChildView1"/>
</i:Interaction.Behaviors>
<local:ChildView1.RenderTransform>
<CompositeTransform/>
</local:ChildView1.RenderTransform>
</local:ChildView1>
</Grid>
</phone:PhoneApplicationPage>
MainViewModel.cs:
public class MainViewModel : ViewModelBase
{
public ICommand ButtonChecked { get; private set; }
public MainViewModel()
{
ButtonChecked = new RelayCommand(() => SwitchVisibility());
}
public void SwitchVisibility()
{
CanShowView = !CanShowView;
RaisePropertyChanged("CanShowView");
}
bool _canShowView = true;
public bool CanShowView
{
get { return _canShowView; }
set
{
if (value != _canShowView)
{
_canShowView = value;
RaisePropertyChanged("CanShowView");
}
}
}
}
ChildView1.xaml:
<UserControl x:Class="myApp.Views.ChildView1"
....
DataContext="{Binding Child1VM, Source={StaticResource Locator}}"
>
<Grid x:Name="HomeGrid">
<Button x:Name=myCloseButton Command="{Binding Child1VM.CloseChildViewCmd, Source={StaticResource Locator}}"/>
...
</Grid>
</UserControl>
Child1ViewModel.cs:
namespace myApp
{
public class Child1ViewModel : ViewModelBase
{
public ICommand CloseChildViewCmd { get; private set; }
public Child1ViewModel()
{
CloseChildViewCmd = new RelayCommand(() => ExecuteCloseChildViewCmd());
}
public void ExecuteCloseChildViewCmd()
{
MainPage mainPage = new MainPage();
MainViewModel mainVM = new MainViewModel();
mainPage.DataContext = mainVM;
mainVM.SwitchVisibility();
}
}
}
注意:我只是一个初学者程序员,所以我解决问题的方式或程序本身可能有问题,因此我愿意接受任何建议,并且非常欢迎代码示例。在进行了大量研究并尝试了许多似乎从未奏效的事情之后,我已经被这个问题困扰了很多天。我花时间详细写了这个问题,我真的希望我能在这里找到解决方案。如果您需要了解更多详细信息,请告诉我。 提前致谢。
编辑:我读到创建视图的新实例不是解决方案。我还能如何访问MainView 的函数并引发属性?
编辑:正如 OmegaMan 所建议的,我使用静态定义来调用我的函数。
所以更新的MainViewModel.cs:
公共类 MainViewModel : ViewModelBase
{
公共 ICommand ButtonChecked { 获取;私人套装; }
私有静态 MainViewModel Primary { 获取;放; }
public MainViewModel()
{
ButtonChecked = new RelayCommand(() => SwitchVisibility());
Primary = this;
}
public void SwitchVisibility()
{
CanShowView = !CanShowView;
RaisePropertyChanged("CanShowView");
}
public static void SwitchViewStatic()
{
Primary.SwitchVisibility();
}
bool _canShowView = true;
public bool CanShowView
{
get { return _canShowView; }
set
{
if (value != _canShowView)
{
_canShowView = value;
RaisePropertyChanged("CanShowView");
}
}
}
}
更新后的Child1ViewModel.cs:
命名空间 myApp
{
公共类 Child1ViewModel:ViewModelBase
{
公共 ICommand CloseChildViewCmd { 获取;私人套装; }
公共 Child1ViewModel()
{
CloseChildViewCmd = new RelayCommand(() => ExecuteCloseChildViewCmd());
}
public void ExecuteCloseChildViewCmd()
{
MainViewModel.SwitchViewStatic();
}
}
}
MyCloseButton 从子视图调用 MainViewModel 中所需的函数,但 CanShowView 仍然不会被提升以允许 DataStateBehavior 监听它,更新 UI...所以我仍然处于僵局。
【问题讨论】:
-
你使用的是哪个 mvvm 框架?
标签: c# wpf xaml windows-phone-7 mvvm