简体   繁体   中英

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>

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?

In WPF, ResourceDictionary I used to set this too:

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

But seems like WindowsPhone can't find DataType property so I commented out this part.

What I'm missing? Any ideas?

Hoooah! Solution found!

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM