-
Notifications
You must be signed in to change notification settings - Fork 9
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
For Avalonia, allow for view models that are in a different assembly from views #34
Comments
If your naming convention isn't the default, then you create your But why so much code? Is there a totally separate problem you're having? That's not very clear. Also, are you working on Desktop or Mobile app, or both? |
I can see I could have been clearer. The naming convention has nothing to do with the problem. If I had the same naming convention as is implemented in Target PlatformsDesktop only. I'm developing my application in Windows, with a view to installation also on MacOS. Summary of Problems
ExampleHave a look at the main branch of my Avalonia application Falcon Programmer. That contains the fix shown in the workaround in my previous post. The main window initially shows several browse buttons that open file or folder dialogs. It works. Try backing out the fix in two stages. First, comment out the Finally, comment out the |
Ah you mean that it's checking for the supplied class name only in the executing assembly? The class really could be in any linked assembly... checking in all of them would require looping through loaded assembly and would have a performance impact. Unless adding a method to the ViewLocator to add the assemblies to look into and loop through those. Also as a note, if you plan to use assembly trimming, you'll need to avoid Reflection and will need to use |
Yes, exactly.
I'm not sure about that. I reckon that, if its not in the executing assembly, it must be in the calling assembly. As I suggested in my original post,
Thanks for the tip! I'd never heard of assembly trimming. I've just looked it up and it does not seem likely that it will become advantageous to implement it in my current project. |
After further investigation, I decided to implement assembly trimming after all, and consequently converted So that works for me. You may still wish to consider enhancing |
It turns out
throws an
Rather than try to find an additional workaround, I have backed out the use of 'Hanuman.MvvmDialogs' from my application, implementing different methods of dialog display and the strongly-typed creation of views required for assembly trimming. |
Why is it trying to convert Why do you say it's a variant of the same problem? |
I have a couple of corrections to my previous comment:
I've made a cutdown version in the hanuman branch of my Avalonia application Falcon Programmer. As it is crashing on startup, I could not test simplifications to the XAML. So I've left the XAML intact and populated the GUI with hard-coded test data rather than accessing a model layer. There are still separate view and view model projects/assemblies. If you get the cutdown version working, you should be able to show open dialogs by clicking the Browse buttons. Message boxes won't show, as validation logic has been removed. However, an error message box could be shown with a simple code change. In |
Summary
I would like to suggest an enhancement for the Avalonia UI implementation to allow for view models that are in a different assembly from views.
Use Case
In my .Net solution, views are in the Views folder of the top-level project/assembly, as in the Avalonia UI MVVM template solution. However, view models are in a separate view model project/assembly, which is referenced by the top-level project/assembly. This is my preference for MVVM applications, as it discourages me and my collaborators from adding references to the view model project that should only be required in the domain of views.
Current Behaviour
HanumanInstitute.MvvmDialogs.Avalonia currently assumes that views are in the same assembly as views. When this is not the case, a
TypeLoadException
is thrown when attempting to show the main window, like this:I have been able to circumvent that by overriding
ViewLocatorBase.GetViewName
andViewLocatorBase.Locate
inViewLocator
. (The code is show below.) With that workaround implemented, anArgumentException
is thrown whenDialogService
attempts to launch a dialog, such as open folder, whose owning view model is aContentControl
, such as aUserControl
. Like this:I have been able to circumvent that by overriding
ViewLocatorBase.CreateViewInstance
inViewLocator
andDialogManager.FindViewByViewModel
in a custom class derived fromDialogManager
. (The code is show below.)Workaround
Implement
ViewLocator
like this. TheGetViewName
override may vary, depending on your view and view model naming standard:Derive a custom class from
DialogManager
, like this:Finally, Instantiate
DialogService
, usually for use inApp.Initialize
, to use the customDialogManager
andViewLocator
. Something like this:Conclusion
It should be possible to enhance the Avalonia UI implementation to allow for view models that are in a different assembly from views. For this to work, if a view cannot be found in the same assembly as the view model (
Assembly.GetExecutingAssembly
), try looking in the calling assembly (Assembly.GetCallingAssembly
).The text was updated successfully, but these errors were encountered: