Ok so I am Using Visual studio and uwp. I created a WeatheVM, to bind to my page, as a Resource. 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.
<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
In the code you gave, I saw that you created an instance of WeathVM
in Page.Resources
, but did not reference it.
You can set the Grid's DataContext to vm so that you can import the resources you created.
<Grid DataContext="{StaticResource vm}">
<!-- Other code -->
</Grid>
Suppose you have a Temp
property to display. After setting the DataContext, you can bind it like this.
<Grid DataContext="{StaticResource vm}">
<TextBlock Text={Binding Temp} />
</Grid>
This is a complete MVVM example , you can modify it with reference to it.
Best regards.
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.