2014-12-13 82 views
1

MainPage.xaml中的Windows Phone 8.1 MVVM设置视图的视图模型

<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').. 

为什么他LL应用程序试图寻找到MainViewModel,而不是CounterViewModel我有一个CounterView,DataContext的内设置?

在WPF中,ResourceDictionary中我用来设置这样太:

<DataTemplate DataType="viewModels:CounterViewModel"> 
    <views:CounterView/> 
</DataTemplate> 

但是好像WindowsPhone的找不到数据类型属性,所以我注释掉这一部分。

我失踪了什么?有任何想法吗?

+0

你试图让自己的视图模型到用户控件? – RenDishen 2014-12-13 11:09:38

+1

也许是因为这是设计时数据上下文而不是实际的数据上下文? – John 2014-12-13 11:21:02

+0

在我的WP 8.1应用程序样式中没有'x:Key'的情况下,如果它们被放置在外部资源字典文件中,它们将被忽略。尝试移动的DataTemplate直接到'' – opewix 2014-12-13 11:28:24

回答

1

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> 

作品像魅力!这是我所做的唯一改变 - 不必做任何事情。

+1

你的答案确实是正确的,但是,你的“错误”的XAML是适合你的设计用户控件,从而使智能感知可以找到您的视图模型的属性。您可以将两者都包含在您的XAML中。 – 2014-12-16 00:23:13

0

http://blog.jerrynixon.com/2013/07/solved-two-way-binding-inside-user.html

貌似答案是博客之内。尚未测试,但这听起来很合理:

Please note: the data context property of the user control inherits from the parent. A DataTemplate might like this. But user controls don’t anticipate a data context type. Instead, they want properties explicitly set. And we want to bind those properties.

随时更新我。 :)