gpt4 book ai didi

c# - Windows Phone 8.1 MVVM设置 View 的 View 模型

转载 作者:行者123 更新时间:2023-12-03 10:36:11 25 4
gpt4 key购买 nike

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

为什么 hell 应用程序尝试查看 MainViewModel 而不是我在CounterView DataContext中设置的 CounterViewModel

在WPF中,我也曾经设置ResourceDictionary:
<DataTemplate DataType="viewModels:CounterViewModel">
<views:CounterView/>
</DataTemplate>

但是似乎WindowsPhone找不到DataType属性,因此我注释掉了这一部分。

我缺少什么?有任何想法吗?

最佳答案

哇!找到解决方案!

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>

奇迹般有效!这是我所做的唯一更改-无需执行其他任何操作。

关于c# - Windows Phone 8.1 MVVM设置 View 的 View 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27458017/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com