[英]ViewModel Called on Navigation Back Prism MVVM Windows Phone 8.1 C#
[英]Windows Phone 8.1 MVVM Setting ViewModel of View
MainPage.xaml中
<phone:PhoneApplicationPage
x:Class="MyApp.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
SupportedOrientations="Portrait" Orientation="Portrait"
shell:SystemTray.IsVisible="True"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:viewModels="clr-namespace:MyApp.ViewModels"
xmlns:views="clr-namespace:MyApp.Views"
mc:Ignorable="d"
d:DataContext="{d:DesignInstance Type=viewModels:MainViewModel}">
<!--FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"-->
<!--LayoutRoot is the root grid where all page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
<!--Pivot Control-->
<phone:Pivot Title="MyApp">
<!--Pivot item one-->
<phone:PivotItem Header="Main">
<Grid>
</Grid>
</phone:PivotItem>
<!--Pivot item two-->
<phone:PivotItem Header="Counter">
<views:CounterView />
</phone:PivotItem>
</phone:Pivot>
</Grid>
</phone:PhoneApplicationPage>
CounterView.XAML
<UserControl x:Class="MyApp.Views.CounterView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:viewModels="clr-namespace:MyApp.ViewModels"
mc:Ignorable="d"
d:DesignHeight="480" d:DesignWidth="480"
d:DataContext="{d:DesignInstance Type=viewModels:CounterViewModel}">
<Grid x:Name="LayoutRoot" Background="Blue" >
<TextBlock Text="{Binding LightSensorInfo}" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="75,137,0,316"/>
</Grid>
</UserControl>
错误:
System.Windows.Data Error: BindingExpression path error: 'LightSensorInfo' property not found on 'MyApp.ViewModels.MainViewModel' 'MyApp.ViewModels.MainViewModel' (HashCode=62333418). BindingExpression: Path='LightSensorInfo' DataItem='MyApp.ViewModels.MainViewModel' (HashCode=62333418); target element is 'System.Windows.Controls.TextBlock' (Name=''); target property is 'Text' (type 'System.String')..
凭啥应用程序试图寻找到MainViewModel代替CounterViewModel我有一个CounterView,DataContext的内设置?
在WPF中,我也曾经设置ResourceDictionary:
<DataTemplate DataType="viewModels:CounterViewModel">
<views:CounterView/>
</DataTemplate>
但是似乎WindowsPhone找不到DataType属性,因此我注释掉了这一部分。
我想念的是什么? 有任何想法吗?
Hoooah! 找到解决方案!
CounterView.XAML
错误:
d:DataContext="{d:DesignInstance Type=viewModels:CounterViewModel}"
正确:
<UserControl.DataContext>
<viewModels:CounterViewModel/>
</UserControl.DataContext>
最后:
<UserControl x:Class="MyApp.Views.CounterView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:viewModels="clr-namespace:MyApp.ViewModels"
mc:Ignorable="d"
d:DesignHeight="480" d:DesignWidth="480">
<UserControl.DataContext>
<viewModels:CounterViewModel/>
</UserControl.DataContext>
<Grid x:Name="LayoutRoot" Background="Blue" >
<TextBlock Text="{Binding LightSensorInfo}" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="75,137,0,316"/>
</Grid>
</UserControl>
奇迹般有效! 这是我所做的唯一更改-无需执行其他任何操作。
http://blog.jerrynixon.com/2013/07/solved-two-way-binding-inside-user.html
答案似乎在该博客中。 尚未测试,但这听起来合乎逻辑:
请注意:用户控件的数据上下文属性是从父级继承的。 一个DataTemplate可能会这样。 但是用户控件无法预期数据上下文类型。 相反,他们希望显式设置属性。 我们想绑定这些属性。
随时更新我。 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.