I made "MVVM flyout" based on this post: https://marcominerva.wordpress.com/2015/01/15/how-to-open-and-close-flyouts-in-universal-apps-using-mvvm/
It is working well. But it is not working with compiled binding(x:Bind)
This:
<Flyout local:FlyoutHelpers.Parent="{x:Bind ShowButton}"...
insead of this:
<Flyout local:FlyoutHelpers.Parent="{Binding ElementName=ShowButton}"...
throws strange error while building:
Error CS1503 Argument 1: cannot convert from 'Windows.UI.Xaml.Controls.Flyout' to 'Windows.UI.Xaml.FrameworkElement'
Is there any option how to use x:Bind?
The problem here is related to the generated code for
{x:Bind}.As we know
{x:Bind}uses generated code to achieve its benefits. And these codes can be found in obj folder, with names like (for C#) <view name>.g.cs. For more info, please see {x:Bind} markup exstrong texttension.If you go to .g.cs file (I used
FlyoutHelperinMainPage,so in my side, it's MainPage.g.cs), you will find the error is inSet_FlyoutDemoSample_FlyoutHelper_Parentmethod. This method is generated at compile time,FlyoutDemoSampleis the namespace of my project. Its name may be different in your side.If we go to the definition of this method, we will find the type of the first parameter in this method is
FrameworkElement.However when using
FlyoutHelper, the parameter we set here is aFlyout.Flyoutclass is not derived fromFrameworkElement. So it throws an error:cannot convert from 'Windows.UI.Xaml.Controls.Flyout' to 'Windows.UI.Xaml.FrameworkElement'. If we change the first parameter's type toDependencyObject, all the codes will work well.However, these codes are generated automatically, if we rebuild this project, we will still get the same error. I'm not sure if this is a potential bug in UWP, but I think there is nothing we can do to fix it. So I'd suggest you still use
Bindingin this special scenario.