none
求MvvMLight指点 RRS feed

  • 问题

  • 我创建了一个WPF工程,使用了MvvMLight。

    如何使用C#获取ViewModel和xaml中的控件 ?

    App.xaml:

    <vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True" xmlns:vm="clr-namespace:HelloMvvmLight.View.ViewModel" />

    ViewModelLocator.cs:

    public ViewModelLocator()
    {
        CreateNavigationService();
    
        SimpleIoc.Default.Register<MainViewModel>();
    
        SimpleIoc.Default.Register<UserInfoWindowViewModel>();
    }
    
    private static void CreateNavigationService()
    {
        ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
    
        var navigationService = new ViewNavigationService();
    
        navigationService.Configure("MainWindow", new Uri(typeof(MainWindow).FullName, UriKind.Relative));
    
        SimpleIoc.Default.Register<IViewNavigationService>(() => navigationService);
    }
    
    public MainViewModel Main
    {
        get { return ServiceLocator.Current.GetInstance<MainViewModel>(); }
    }

    MainWindow.xaml:

    <Window.DataContext>
        <Binding Path="Main" Source="{StaticResource Locator}"></Binding>
    </Window.DataContext>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
    
        <Frame x:Name="MainFrame"
                NavigationUIVisibility="Hidden"
                ScrollViewer.CanContentScroll="False"
                HorizontalAlignment="Stretch" HorizontalContentAlignment="Center"
                VerticalAlignment="Stretch" VerticalContentAlignment="Center"/>
    
        <Button x:Name="btnNavigate" Grid.Row="1" Command="{Binding ToPageOneCommand}">Page One</Button>
    </Grid>


    1. 在MainWindow.cs 如何使用C#获取MainViewModel?

    2. 在MainViewModel.cs 或者ViewModelLocator.cs中如何使用C#获取Button(名字为"btnNavigate"的按钮)?

    3. 在MainWindow.cs或其他的page.cs中如何使用C#获取ViewModelLocator?


    2017年8月4日 15:55

答案


  • Hi oneonce,

    你可以访问MVVM Light官网获取更多的信息。

    MVVM Light Toolkit:
    http://www.mvvmlight.net/

    如果你有关于MVVM Light的问题,他们在StackOverflow 有支持中心,你可以到那边去提交你的问题。


    Best Regards,

    Yohann Lu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 oneonce 2017年8月8日 2:55
    2017年8月7日 10:16