[英]Windows Phone 8.1 MVVM Setting ViewModel of View
MainPage.XAML 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 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>
Error: 错误:
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')..
Why the hell Application tries to look into MainViewModel instead of CounterViewModel which I had set within a CounterView, DataContext? 凭啥应用程序试图寻找到MainViewModel代替CounterViewModel我有一个CounterView,DataContext的内设置?
In WPF, ResourceDictionary I used to set this too: 在WPF中,我也曾经设置ResourceDictionary:
<DataTemplate DataType="viewModels:CounterViewModel">
<views:CounterView/>
</DataTemplate>
But seems like WindowsPhone can't find DataType property so I commented out this part. 但是似乎WindowsPhone找不到DataType属性,因此我注释掉了这一部分。
What I'm missing? 我想念的是什么? Any ideas? 有任何想法吗?
Hoooah! Hoooah! Solution found! 找到解决方案!
CounterView.XAML CounterView.XAML
Wrong: 错误:
d:DataContext="{d:DesignInstance Type=viewModels:CounterViewModel}"
Correct: 正确:
<UserControl.DataContext>
<viewModels:CounterViewModel/>
</UserControl.DataContext>
Final: 最后:
<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>
Works like a charm! 奇迹般有效! This is the only change I made - didn't have to do anything else. 这是我所做的唯一更改-无需执行其他任何操作。
http://blog.jerrynixon.com/2013/07/solved-two-way-binding-inside-user.html http://blog.jerrynixon.com/2013/07/solved-two-way-binding-inside-user.html
Looks like the answer is within that blog. 答案似乎在该博客中。 Not tested yet, but it sounds logical: 尚未测试,但这听起来合乎逻辑:
Please note: the data context property of the user control inherits from the parent. 请注意:用户控件的数据上下文属性是从父级继承的。 A DataTemplate might like this. 一个DataTemplate可能会这样。 But user controls don't anticipate a data context type. 但是用户控件无法预期数据上下文类型。 Instead, they want properties explicitly set. 相反,他们希望显式设置属性。 And we want to bind those properties. 我们想绑定这些属性。
Feel free to update me. 随时更新我。 :) :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.