问题描述
我是MVVM架构的新手。我正在构建一个UWP应用。我基本上有一个View(XAML),后面的代码(Xaml.cs),ViewModel和数据服务。
我的View / XAML看起来像这样:
<Page
x:Class="SnapBilling.SyncModule.SyncView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:controls="using:Microsoft.Toolkit.Uwp.UI.Controls"
mc:Ignorable="d" Loaded="Page_Loaded"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid>
<Grid.Resources>
<!--DataTemplate for Published Date column defined in Grid.Resources. PublishDate is a property on the ItemsSource of type DateTime -->
<DataTemplate x:Key="ProgressTemplate" >
<ProgressBar x:Name="progressBar1" Value="{Binding Value.ProgressPercentage}" Maximum="100" Margin="20"/>
</DataTemplate>
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition Height="4*" />
<RowDefinition Height="2*" />
</Grid.RowDefinitions>
<controls:DataGrid x:Name="BackupSummaryDataGrid"
Grid.Row="0"
Margin="40"
ItemsSource="{x:Bind DataContext.UploadProgressInfoDict}"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
AlternatingRowBackground="Transparent"
AreRowDetailsFrozen="False"
AreRowGroupHeadersFrozen="True"
AutoGenerateColumns="False"
CanUserReorderColumns="True"
CanUserResizeColumns="True"
ColumnHeaderHeight="32"
FrozenColumnCount="0"
GridLinesVisibility="None"
HeadersVisibility="Column"
HorizontalScrollBarVisibility="Visible"
IsReadOnly="False"
MaxColumnWidth="400"
RowDetailsVisibilityMode="Collapsed"
RowGroupHeaderPropertyNameAlternative="Range"
SelectionMode="Extended"
VerticalScrollBarVisibility="Visible"
>
<controls:DataGrid.Columns>
<controls:DataGridTextColumn Tag="SyncType" Header="Sync Type" Binding="{Binding Key}" IsReadOnly="True" />
<controls:DataGridTextColumn Tag="remaining" Header="Pending Items" Binding="{Binding Value.remainingNow}" IsReadOnly="True" />
<controls:DataGridTemplateColumn Header="% remaining" CellTemplate="{StaticResource ProgressTemplate}" />
<controls:DataGridTextColumn Header="Status" Binding="{Binding Value.ProgressMessage}" IsReadOnly="True"/>
</controls:DataGrid.Columns>
</controls:DataGrid>
</Grid>
</Page>
现在,当页面/视图xaml加载时,它将调用xaml类的构造函数,在此我们初始化组件并使用视图模型对象设置数据上下文。这是类的代码,
public sealed partial class SyncView : Page,IView
{
public SyncView()
{
this.InitializeComponent();
DataContext = new SyncViewModel(ServiceLocator.Current.GetService<ICommonServices>());
}
private void Page_Loaded(object sender,RoutedEventArgs e)
{
}
}
现在,DataContext = new SyncViewModel(ServiceLocator.Current.GetService<ICommonServices>());
在这里创建了一个ViewModel对象,并将其正确绑定到数据上下文。
问题
当我通过Page_Loaded
事件而不是像下面的构造函数那样设置数据上下文时,viewmodel对象未绑定到页面的数据上下文。
private void Page_Loaded(object sender,RoutedEventArgs e)
{
DataContext = new SyncViewModel(ServiceLocator.Current.GetService<ICommonServices>());
}
如何解决这个问题?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)