Application
x:Class="sqlasynctest.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:sqlasynctest"
xmlns:viewModel="using:sqlasynctest.ViewModel"
RequestedTheme="Light">
<Application.Resources>
<ResourceDictionary>
<viewModel:ViewModelLocator x:Key="Locator" />
</ResourceDictionary>
</Application.Resources>
public class ViewModelLocator
{
public ViewModelLocator()
{
ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
if (!SimpleIoc.Default.IsRegistered<IDal>())
{
SimpleIoc.Default.Register<IDal, Dal>();
}
SimpleIoc.Default.Register<MainViewModel>();
}
public MainViewModel Main
{
get
{
return ServiceLocator.Current.GetInstance<MainViewModel>();
}
}
public static void Cleanup()
{
// TODO Clear the ViewModels
}
}
MainViewModel
public class MainViewModel : ViewModelBase
{
private readonly IDal _iDal;
private IList<User> _user;
public IList<User> Users
{
get { return _user; }
set { Set("Users", ref _user, value); }
}
public MainViewModel(IDal dal)
{
_iDal = dal;
LoadDataCommand = new RelayCommand(async () => await LoadData());
}
public ICommand LoadDataCommand { get; private set; }
public async Task LoadData()
{
Users = await _iDal.LoadAllUser();
}
}
的MainPage
<Page
x:Class="sqlasynctest.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:sqlasynctest"
DataContext="{Binding Main, Source={StaticResource Locator}}"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="20"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="140"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<ListBox Grid.Column="0" Grid.Row="1" SelectionMode="Single"
ItemsSource="{Binding Users}" Margin="120,0,0,40">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding FirstName}"/>
<TextBlock Text="{Binding LastName}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
表和方法很好,但我认为绑定是错误的,帮助我 LoadData() 方法不会在任何地方调用。
我得到了一个解决方案
protected override void OnNavigatedTo(NavigationEventArgs e)
{
var viewModel = (MainViewModel)DataContext;
if (viewModel != null)
{
viewModel.LoadData();
}
我不想触摸MainPage.cs 在xaml代码中执行此操作的任何其他方法
答案 0 :(得分:0)
您的问题是您的收藏品是List
。它不会通知UI有关chnages的信息。用户ObservableCollection
代替它,它将开始工作。
答案 1 :(得分:0)
您的视图模型中有LoadDataCommand
,但不会从任何地方调用它。这里没有魔法 - 你必须从XAML或代码隐藏文件调用它。以下是在XAML中完成此操作的方法:
Microsoft.Xaml.Behaviors.Uwp.Managed
NuGet包。修改主页的XAML:
<Page
...
xmlns:i="using:Microsoft.Xaml.Interactivity"
xmlns:c="using:Microsoft.Xaml.Interactions.Core">
<i:Interaction.Behaviors>
<c:EventTriggerBehavior EventName="Loaded">
<c:InvokeCommandAction Command="{Binding LoadDataCommand}"/>
</c:EventTriggerBehavior>
</i:Interaction.Behaviors>
<Grid ...>
从代码隐藏文件中删除重写的OnNavigatedTo
方法。
LoadDataCommand
时都会调用MainPage
。