Prism: Using Xamarin.Forms Previewer with Page that has ViewModelLocaltor enable throws at design-time

Created on 10 May 2017  Â·  8Comments  Â·  Source: PrismLibrary/Prism

Using Xamarin.Forms Previewer with Page that has ViewModelLocaltor enable throws at design-time

Create a new XF project using Prism

Enable ViewModelLocator on the MainPage

Build the solution

Open the VS XF Previewer

Exception throw.

Discussion:
VML needs to handle design-time scenarios in XF. The code can't throw an exception at design-time. If Xamarin can't provide reliable design-time API, this code will have to handle this scenario and not throw.

XF

Most helpful comment

The default behavior works fine for me. The only issue that was noticed is that the App.xaml.cs requires an empty default ctor. I added this, and everything worked fine:

public App() : this(null) { }

I found no issues of exceptions in the previewer with VML. The bindings worked and displayed all the default values defined for the properties.

When PrismAplication was written, I did not consider anything creating it using reflection, so I was not concerned with having a default ctor. Since it is obvious that the Previewer needs this, I will update the PrismApplication class to have a default ctor.

All 8 comments

@kdawg1406 I'm not sure what exactly you're doing but, I can File -> New a project add the ViewModelLocator to a View and I get no exception thrown by the Xamarin Forms Previewer.

Can you provide some more information like which versions of Prism, Xamarin Forms and your IDE.

Use AppMap to create the solution.

On May 10, 2017 9:53 PM, "Dan" notifications@github.com wrote:

@kdawg1406 https://github.com/kdawg1406 I'm not sure what exactly
you're doing but, I can File -> New a project add the ViewModelLocator to a
View and I get no exception thrown by the Xamarin Forms Previewer.

Can you provide some more information like which versions of Prism,
Xamarin Forms and your IDE.

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/PrismLibrary/Prism/issues/1037#issuecomment-300660495,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AFXQT17_p-0-mvzqkiZ1NJkuzjkq3sOOks5r4moYgaJpZM4NWmGP
.

@kdawg1406 if you remove the ViewModelLocator does the Previewer in VS succeed?

Yes

Ok... with the Attached property removed try the following.

public class ViewModelLocatorExtension : IMarkupExtension
{
    public object View { get; set; }

    public object ProvideValue(IServiceProvider serviceProvider)
    {
        object viewModel = null;
        ViewModelLocationProvider.AutoWireViewModelChanged(View, (v, vm) => viewModel = vm);
        return viewModel;
    }
}
xmlns:xaml="{local import to the above class}"
x:Name="view"
BindingContext="{xaml:ViewModelLocator {x:Reference view}}"

The default behavior works fine for me. The only issue that was noticed is that the App.xaml.cs requires an empty default ctor. I added this, and everything worked fine:

public App() : this(null) { }

I found no issues of exceptions in the previewer with VML. The bindings worked and displayed all the default values defined for the properties.

When PrismAplication was written, I did not consider anything creating it using reflection, so I was not concerned with having a default ctor. Since it is obvious that the Previewer needs this, I will update the PrismApplication class to have a default ctor.

Actually, this isn't something that needs to be done in Prism itself. The templates just have to be updated to provide the ctor snippet.

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

craigblazakis picture craigblazakis  Â·  28Comments

diego2k picture diego2k  Â·  24Comments

huyjack178 picture huyjack178  Â·  24Comments

cmjacobs picture cmjacobs  Â·  31Comments

mohammedmsadiq picture mohammedmsadiq  Â·  45Comments