-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
NativeMenu binding for "About" on MacOS not working? #8013
Comments
Sounds like a wrong Datacontext to me. Maybe tr to use CompiledBindings to debug you Bindings. Or use a static command instead. You could access it with Happy coding |
Yes but from that link: So you can't use compiled bindings for a command :( |
Is there a way to just replace the menu item with an OnClick event or something that does not require binding? I've tried without success, but thought it's worth asking |
You can for ICommand as you have it. You can't use it for binding to methods. |
Unfortunately it doesn't seem to help, it needs a data type and when I specify it it says it can't find it, so I'm obviously missing something. Probably because I've just used a code behind model for my project, so maybe there is view model code I've not setup correctly. As I asked, do you know if there is a way to do it without binding? I just need it to open a window - an on click code behind would be simple, but I can't get that to work either! I can't use x:Name or Click or Clicked... it doesn't recognise any of those for the NativeMenuItem |
Yes you need a ViewModel set as DataContext for Binding. Or you use x:Static instead of Binding. |
do you know the syntac for x:Static? I've not found any articles that show an example |
It's for WPF but should work similar https://docs.microsoft.com/en-us/dotnet/desktop/xaml-services/xstatic-markup-extension |
So something like
? |
I thought I'd go back and make the example ToDo app and see if I can get the About box to work there... but downloading and building that with .net5.0 as a target - it can't bind to it's content todo-tutorial-master/Todo/Views/MainWindow.xaml(17,17): Error: XLS0505: Type 'Binding' is used like a markup extension but does not derive from MarkupExtension. So I think I'll just remove the about box for now. This seems like something that is far more complicated than it should be. |
Note that AboutCommand had to be static. |
Yes, I've made a static class, but I can't get the compiler to find it :( |
Got it to find it:
but now it's not the correct type:
|
Remove the Binding as i wrote above.
|
Oh, I didn't realize you could do it like that. So it's not binding? That now throws a different exception - so it's progress! "The type initializer for 'DApps.Data.NativeMenuModel' threw an exception."
but it seems unrelated... it's System.Collections.Generic.KeyNotFoundException Which seems totally unrelated? Oh - it seems it's trying to load the new AboutDialog but can't find the static resources... Using the static class seems very dodgy.... |
That's a different issue inside you Dialog. I would like to close this issue as it's no bug. For questions like these there is telegram, gitter, discord and the discussion section. |
If you say so. I'm not convinced the static function is the correct way to get this to work. I would suggest a simple example of how to override the default Avalonia About box would be very useful for any mac developers. |
I went back to see if I could work out if I could get the Binding to work.
I put CompiledBinding in and this compiles without error and the App class has the public AboutCommand
Yet this still results in [Binding] Error in binding to 'Avalonia.Controls.NativeMenuItem'.'Command': 'Null value in expression ''.' (NativeMenuItem #6480969) So I'm not convinced this isn't a bug |
You again have no DataContext set. This is not how MVVM works. |
I still think a static command would be ok in your case. If not, you need to set somehow your App as the DataContext of your Menu and I don't really see how this should work. |
Ok, the issue can now be closed. I've finally figured out all the parts that need to be implemented. I'll include sample code here so that if anyone else is having difficulty they have a reference. I still feel that this is overly complicated to replace the AboutAvalonia native menu item and there should be a simpler way to achieve this in a non MVVM application. Firstly, create a ViewModelBase class:
Then create an AppViewModel class with your AboutCommand public property
Create your AboutDialog.axaml
and code behind class for the about dialog (the code behind can be much simpler than I have here - this is from the code in the avaloina source)
And finally at the top of App.axaml
and in the code behind App.axaml.cs
I hope this might help someone else trying to implement this simple feature. |
MiniMVVM seems to no longer exist. I'm not sure where you're getting MiniCommand from. |
@majeric it's their command implementation. You are free to choose any MVVM library. Like CommunityToolkit.MVVM or ReactiveUI. Or just handle Click event without any commands/mvvm. |
I'm trying to update the About window in the MacOS version of my application.
Following this post:
#3541
The problem is that I can't work out how to bind the AboutCommand
So this is placed in my App.axaml file and then I've added
in the Initialize() of App.axaml.cs but I get
[Binding] Error in binding to 'Avalonia.Controls.NativeMenuItem'.'Command': 'Null value in expression ''.' (NativeMenuItem #6480969)
I do this and the menu is correctly display "About My App" - but it's greyed out.
I've also tried placing the Native Menu in the MainWindow.axaml file, but that doesn't override it (the default Avalonia About box appears).
I'm obviously just misunderstanding how to implement this. Any advice would be greatly appreciated.
The text was updated successfully, but these errors were encountered: