Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using bindings for MAUI controls causes view to not be rendered at all #21

Open
gentledepp opened this issue May 6, 2024 · 10 comments
Open

Comments

@gentledepp
Copy link

gentledepp commented May 6, 2024

Hello... me again.

I am really sorry for bothering you guys, but I keep bumping into stuff.

I am currently trying to "copy" the implementation of the sample of ZXing Maui Barcode Reader

This is the view I try to move to Avalonia:
https://github.com/Redth/ZXing.Net.Maui/blob/main/BigIslandBarcode/MainPage.xaml

I managed to get it to work and actually get rendered.

1st attempt - keep everything in Maui

This worked on my android emulator (API 30)
but on my physical device (Huawei p30 pro) the UI remains empty

2nd attempt - use avalonia, and only wrap the barcodescanner in host

Now, it renders the barcode scanner view,
but as soon as I add any binding to those controls (even for bool values), the UI is not rendered anymore - the screen stays brown which is the background color of the Avalonia MainView
image

Am I missing something here?

Additionally, the rendering seems to not work correctly either:
The camera view should be shown inside the empty space here
image

But is actually rendered on top/covering everything else on the emulator:
image

It works though on my device... :-|

@maxkatz6
Copy link
Member

maxkatz6 commented May 7, 2024

I don't know why it stops rendering the native control once binding is added, but I can say for sure that bindings aren't supported here.
It's actually pretty similar to #20 issue.

In Avalonia XAML, bindings and xName works well, since all the elements are AvaloniaObjects. The same goes with MAUI, it all works while all elements are known to MAUI framework.

But when we mix different frameworks, it gets more complicated. You can't apply Avalonia.Binding to Maui.Control, since it doesn't inherit AvaloniaObject.

Known workaround is to create a separated MAUI Page (or ContentView) and setup MAUI Bindings there. And in Avalonia XAML file only include this Page, without any extra bindings.
This way, you will have one MAUI XAML file with MAUI bindings, and another Avalonia XAML file that has first one as a content.

@maxkatz6
Copy link
Member

maxkatz6 commented May 7, 2024

And to be clear, while it's technically possible to add x:Name support for MAUI elements, I don't think we can support bindings on MAUI controls, without making adding MAUI as a first-class support to our XAML compiler.

@maxkatz6
Copy link
Member

maxkatz6 commented May 7, 2024

Ideally, ZXing.Net.Maui should be re-ported to Avalonia using NativeControlHost, avoiding any other frameworks in between.
Hard to tell, how difficult it would be though. Technically, only camera view (a native control) needs to be wrapped into a NativeControlHost.

@gentledepp
Copy link
Author

gentledepp commented May 7, 2024

yeah.. I aggree...

Regarding the preview overlapping everything else:
This seems to be a zxing.maui issue: If I run their demo project and try to put the camera preview into the middle row, it still overlaps.
Redth/ZXing.Net.Maui#4

The difference is, that AvaloniaMauiHybrid always renders all MAUI controls on top of everything else.
So I cannot render any avalonia controls on top of MAUI, correct?
Or is that a bug?

@maxkatz6
Copy link
Member

maxkatz6 commented May 7, 2024

On overlapping issue - the same would be with Avalonia-only port. Since native controls would be rendered on top of any avalonia content.

So I cannot render any avalonia controls on top of MAUI, correct?

So, yes. That's correct.
We have plans to support Popup (and Popup-related controls, like tooltip or flyout) to work on a separated layer though. It's already done on desktop (where it often is a separated window).

@gentledepp
Copy link
Author

Ok... makes sense.
Now, I updated the sample, so that the MAUI controls are in a separate MAUI View.
They share the same BindingContext/DataContext (QRCodeViewModel) theough.

But: Again the bindings do not have any effect on the MAUI controls.
Any thoughts?

@maxkatz6
Copy link
Member

maxkatz6 commented May 8, 2024

They share the same BindingContext/DataContext (QRCodeViewModel) theough.

Looks like DataContext isn't passed automatically.
But it's something that should be handled by this library.

A workaround to add (in QRCodeView.axaml.cs file):

protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
    base.OnPropertyChanged(change);

    if (change.Property == DataContextProperty)
    {
        ProgressBarHost.Content!.BindingContext = change.NewValue;
        BarcodeViewHost.Content!.BindingContext = change.NewValue;
    }
}

I also added BarcodeViewHost x:Name on the BarcodeView control host.

@gentledepp
Copy link
Author

Apologies... I could have come to that conclusion myself... 😕
Thank you so much for all your valuable responses!

@gentledepp
Copy link
Author

Sorry - me again.
Now I tried out using ReactiveUI within MAUI controls.

For this, I started from the functioning prototype, where the MAUI UI parts are split out into their own views, so the MAUI bindings will work.
Everything works fine.

Now, i changed the ContentViews to become ReactiveContentView<>s and wanted to check if WhenActivated() was ever called.
It turns out, that, again, the UI simply isn't rendered anymore.

You can check out the branch here: https://github.com/gentledepp/POC_AvaloniaMauiApp01/tree/poc/settingmauipropsincodebehind

So is this now a bug or again some known shortcoming?

@gentledepp
Copy link
Author

Additionally this POC has two demo viewmodels:
PicturesViewModel and QRCodeViewModel.
To "navigate" between them, the MainViewModel contains a ContentControl and a command that just swaps the CurrentViewModel
image

Interestingly, navigating from the QRCodeView to the PicturesView causes the PicturesView to (again) not be rendered at all:

truncation

Note: This also "works" when

  • starting out with the QRCodeViewModel
  • or re-using the viewmodel instances

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants