How to do a simple ViewModel first approach in UWP
Asked Answered
C

1

10

I haven't really found my answer to my problem anywhere, so I really hope that someone actually was solving similar problem before me.

I'm developing an UWP application after I was messing with MVVM stuff in WPF for like a year (no widespread frameworks/libraries, just very basic stuff to get the grasp of it). I've come to the realization that I can't really recreate my beloved patterns in UWP, because it has tighter rules than WPF.

What I did in WPF: What I had was few VMs with INPC and properties, etc. and Views with bindings, usual stuff. It was coupled together by the magic of DataTemplate and a really simple method:

    public static void Show(this ViewModelBase vm)
    {
        CreateWindow(vm).Show();
    }

    private static Window CreateWindow(ViewModelBase vm)
    {
        return new Window
               {
                   Icon = Resources.app_icon.ToImageSource(),
                   Width = 500d,
                   Height = 320d,
                   Content = vm,
                   Title = vm.ToString()
               };
    }

In my ResourceDictionary, I defined all templates for my ViewModel classes, like so:

<DataTemplate DataType="{x:Type local:RandomViewModel}">
    <local:RandomView />
</DataTemplate>

UWP situation: I started with a ViewModels that derived from ContentControl, which worked as a prototype, but was ugly and severely broke MVVM "rules" (imho). I switched to the DataTemplate ResourceDictionary with underlying static class that I saw on Build (2016?) conference, which led me to my current state. DataTemplate now has to have a x:Key, which prevents me to leave everything so loosely coupled. Of course the DataTemplate works if I explicitly use the key for ContentTemplate, but that's something I wanted to avoid.

After few weeks of thinking and contemplating and being all frustrated with it, I finally decided to ask someone to at least give me a hint of how to deal with this. For this specific application, I don't really want to go down the road of "do-everything" framework. I also wanted to use compiled binding, so the whole Content/DataContext business is gone also.

Is there a elegant (and simple) or obvious solution that I've missed? I'd be really thankful for any useful tips.

Edit: I know that the question is very vague, If I knew exactly what to ask for, I'd probably keep searching the internet / trying to figure things out by myself. I hope you don't mind.

Cicily answered 8/2, 2017 at 20:49 Comment(4)
It seems like your looking for a View Locator and/or ViewModel Locator, here's a good post that might get you going Navigation Between Pages Using MVVM Light in Universal Windows Platform(UWP)Hydromagnetics
Another decent one, bit more basic though A Minimal MVVM UWP AppHydromagnetics
Both of these link are really great and I will probably take ideas from both. This might actually get me moving forward with my app again. Thanks a lot!Cicily
This is a good question. Not sure why is it downvoted.Fetial
B
-1

Are you forced to use UWP? If not I would really recommend using MAUI instead. There you can use your MVVM approach with ease. And what's great about it. Once you build the MAUI app you can so simply deploy it to Android, Linux, IOS and Windows. Even though the ecosystem is still weak for MAUI I am sure that this is the way to go for any non-web applications on microsoft tech-stack. And I personaly like it much more than i.e. Flutter.

Badmouth answered 2/5, 2023 at 9:31 Comment(2)
I asked this question back in 2017 when MAUI didn't even exist. You haven't even brushed the problem of ViewModel or Model first approach and your answer feels more like and advert. I know what the current GUI trends are.Cicily
Sorry for trying to help. ;-)Badmouth

© 2022 - 2024 — McMap. All rights reserved.