UWP Prism ViewModel เป็นโมฆะ

การใช้เทมเพลต Visual Studio 2017 Universal Windows ฉันสร้างแอปทดสอบ UWP โดยใช้ Prism ทุกอย่างทำงานได้ดีจนกว่าฉันจะเพิ่มหน้าว่างใหม่ให้กับแอป มุมมองนี้เรียกว่า:

AbcPage

XAML

<Page
x:Class="UI_Test_1.Views.AbcPage"
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:local="using:UI_Test_1.Views"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:prismMvvm="using:Prism.Windows.Mvvm"
prismMvvm:ViewModelLocator.AutoWireViewModel="True"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
mc:Ignorable="d">

<Grid>
    <Button Click="Button_Click" Content="test" />
</Grid>

I added the

 xmlns:prismMvvm="using:Prism.Windows.Mvvm"
 prismMvvm:ViewModelLocator.AutoWireViewModel="True"

รหัสด้านหลังจึงเป็นดังนี้:

 namespace UI_Test_1.Views
{
   public sealed partial class AbcPage : Page
   {
      AbcPageViewModel viewModel => DataContext as AbcPageViewModel;
      public AbcPage()
      {
          this.InitializeComponent();
      }
      private void Button_Click(object sender, RoutedEventArgs e)
      {
         var vm = viewModel;//this is null
       }
  }
}

และในที่สุด ViewModel ของฉัน:

namespace UI_Test_1.ViewModels
{
   public class AbcPageViewModel : ViewModelBase
   {
       public AbcPageViewModel()
       {
       //never called
        }
    }
 }

แบบแผนดูเหมือนจะถูกต้องหรือฉันทำผิดไป? ทำไมถึงเป็น

 AbcViewModel

โมฆะ? ฉันจะแก้ไขข้อบกพร่องนี้ได้อย่างไร


person Paul Stanley    schedule 14.11.2018    source แหล่งที่มา
comment
คุณต้องเพิ่ม AbcPageViewModel ของคุณในโฟลเดอร์ ViewModels ซึ่งเป็นที่ที่ปริซึมจะพยายามค้นหาตามค่าเริ่มต้น   -  person Dishant    schedule 15.11.2018
comment
AbcPageViewModel อยู่ในโฟลเดอร์ ViewModels   -  person Paul Stanley    schedule 15.11.2018
comment
คุณใช้ปริซึมเวอร์ชันใด?   -  person Nico Zhu - MSFT    schedule 15.11.2018
comment
อนิจจาไม่มีรหัสเช่นนั้นในโครงการของฉัน App.xaml.cs มีรูทีนการเริ่มต้นและคุณสามารถกำหนดค่าคอนเทนเนอร์สำหรับ Ioc ได้ แต่ไม่มีอะไรเหมือนกับลิงก์ ขอบคุณ   -  person Paul Stanley    schedule 15.11.2018
comment
อืม ปัญหาได้รับการแก้ไขแล้วเหรอ?   -  person Nico Zhu - MSFT    schedule 15.11.2018
comment
ฉันใช้เทมเพลตที่มาพร้อมกับ Visual Studio 15.8.9 Prism Unity 7.1.0.431 ตาม ti Nuget   -  person Paul Stanley    schedule 15.11.2018
comment
ให้เราสนทนาต่อในการแชท   -  person Nico Zhu - MSFT    schedule 15.11.2018


คำตอบ (2)


สำหรับการใช้ปริซึมรุ่นแรกๆ ภายใน uwp คุณต้องกำหนดค่าฐาน ting เพิ่มเติมสำหรับโปรเจ็กต์ uwp ดั้งเดิม เช่น App class และ Page class แน่นอนว่า ทางการได้ให้ ตัวอย่างโค้ด ที่คุณสามารถทำได้ อ้างอิง.

public sealed partial class App : PrismUnityApplication
{
    public App()
    {
        InitializeComponent();
    }

    protected override UIElement CreateShell(Frame rootFrame)
    {
        var shell = Container.Resolve<AppShell>();
        shell.SetContentFrame(rootFrame);
        return shell;
    }

    protected override Task OnInitializeAsync(IActivatedEventArgs args)
    {
        Container.RegisterInstance<IResourceLoader>(new ResourceLoaderAdapter(new ResourceLoader()));
        return base.OnInitializeAsync(args);
    }

    protected override Task OnLaunchApplicationAsync(LaunchActivatedEventArgs args)
    {
        NavigationService.Navigate(PageTokens.Main.ToString(), null);
        return Task.FromResult(true);
    }
}

สำหรับเวอร์ชัน 7.2 ล่าสุดที่ไม่ได้เผยแพร่จะมีโหมดการใช้งานใหม่ หากต้องการข้อมูลเพิ่มเติม โปรดตรวจสอบลิงก์นี้

sealed partial class App : PrismApplication
{
    public static IPlatformNavigationService NavigationService { get; private set; }

    public App()
    {
        InitializeComponent();
    }

    public override void RegisterTypes(IContainerRegistry container)
    {
        container.RegisterForNavigation<MainPage, MainPageViewModel>(nameof(Views.MainPage));
    }

    public override void OnInitialized()
    {
        NavigationService = Prism.Navigation.NavigationService
            .Create(new Frame(), Gestures.Back, Gestures.Forward, Gestures.Refresh);
        NavigationService.SetAsWindowContent(Window.Current, true);
    }

    public override void OnStart(StartArgs args)
    {
        NavigationService.NavigateAsync(nameof(Views.MainPage));
    }
}
person Nico Zhu - MSFT    schedule 15.11.2018

ฉันทำผิดพลาดกับแบบแผนการตั้งชื่อ หากเพจของคุณคือ AbcPage viewmodel ควรเป็น AbcViewModel ไม่ใช่ AbcPageViewModel

person Paul Stanley    schedule 15.11.2018