简体   繁体   English

通用 Windows 平台问题绑定中的 MVVM

[英]MVVM in Universal Windows Platform problem binding

Ok so I am Using Visual studio and uwp.好的,所以我正在使用 Visual Studio 和 uwp。 I created a WeatheVM, to bind to my page, as a Resource.我创建了一个 WeatheVM,以作为资源绑定到我的页面。 Evrerytime I create the Resource, it say that "Cannot create WeatheVM" I am very frustrated, I don't understand why is not letting me, I need this to access my properties.每次我创建资源时,它都说“无法创建 WeatheVM”我很沮丧,我不明白为什么不让我,我需要这个来访问我的属性。

<Page x:Class="uwpMVVM.View.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:local="using:uwpMVVM.View"
      xmlns:vm="using:uwpMVVM.ViewModel"
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
      mc:Ignorable="d"
      Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <Page.Resources>
        <vm:WeatherVM x:Key="vm" />
    </Page.Resources>

    <Grid>

        <AutoSuggestBox x:Name="box"
                        Grid.Column="1"
                        Margin="40"
                        QueryIcon="Find"
                        />
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="53*" />
            <ColumnDefinition Width="247*" />
        </Grid.ColumnDefinitions>

    </Grid>
</Page>

by the way my VM is a public class顺便说一句,我的虚拟机是公共 class

In the code you gave, I saw that you created an instance of WeathVM in Page.Resources , but did not reference it.在您提供的代码中,我看到您在WeathVM中创建了一个Page.Resources实例,但没有引用它。

You can set the Grid's DataContext to vm so that you can import the resources you created.您可以将 Grid 的 DataContext 设置为 vm,以便您可以导入您创建的资源。

<Grid DataContext="{StaticResource vm}">
    <!-- Other code -->
</Grid>

Suppose you have a Temp property to display.假设您有一个Temp属性要显示。 After setting the DataContext, you can bind it like this.设置好DataContext后,就可以这样绑定了。

<Grid DataContext="{StaticResource vm}">
    <TextBlock Text={Binding Temp} />
</Grid>

This is a complete MVVM example , you can modify it with reference to it.这是一个完整的 MVVM 示例,您可以参考它进行修改。

Best regards.此致。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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