XAML MVVM Binding Extensions to simplify Markup and integrate VIEW with VIEWMODEL and ViewModel Attributes #237
Replies: 20 comments
-
Hello, 'cesarchefinho! Thanks for submitting a new feature request. I've automatically added a vote 👍 reaction to help get things started. Other community members can vote to help us prioritize this feature in the future! |
Beta Was this translation helpful? Give feedback.
-
also for previous example here is your viewmodel public partial class ExtendedObservableValidator : ObservableValidator
{
// ... lot of goodies
// but
[IndexerName("Item")]
public string this[string columnName] =>
string.Join(Environment.NewLine,
(from ValidationResult e
in GetErrors(columnName)
select e.ErrorMessage));
public void OnErrorsChanged(object sender, DataErrorsChangedEventArgs e)
{
if (string.IsNullOrWhiteSpace(e.PropertyName))
{
OnPropertyChanged("Item[]");
}
else
{
OnPropertyChanged($"Item[{e.PropertyName}]");
}
}
}
public partial class UserViewModel : ExtendedObservableValidator
{
[Display(Name = "Username", Description = "Your Username in format 999.999.999-99")]
[DisplayFormat(DataFormatString = "999.999.999-99")]
[NotEmpty(ErrorMessage = "Username Is Required", EmptyValue = " . . - ")]
[StringLength(maximumLength: 14, MinimumLength = 11, ErrorMessage = "Username must be 14 or 11 digits !")]
[ObservableProperty]
private string username = String.Empty;
[Display(Name = "Password", Description = "Type your password Here")]
[Password]
[Required(ErrorMessage = "Por favor, digite a senha.")]
[StringLength(maximumLength: 16, MinimumLength = 6, ErrorMessage = "Password must be at minumum 6 chars e maximum of 16 chars !")]
[ObservableProperty]
private string passwordProp = String.Empty;
} |
Beta Was this translation helpful? Give feedback.
-
if someone from Microsoft aprove this especification I will submit source code here |
Beta Was this translation helpful? Give feedback.
-
@cesarchefinho we're close to opening up our Toolkit Labs CommunityToolkit/WindowsCommunityToolkit#4487. That'll be a place to start a proposal for something like this and gather more feedback/input from the community on an implementation. Though we can gather some of that here now. @Arlodotexe @Sergio0694 @mrlacey would be interested in your thoughts. In general though from my perspective, part of the massive effort we put towards creating the MVVM Toolkit into the Community Toolkit was around trying to keep things simple and being a reference implementation on top of the base C# interfaces. Something that was easy for folks to pick up and use in whichever pieces they need. Having a more specific platform abstraction that imposes a specific way of doing things goes against those principles we adopted though. It could also be confusing to have two types of MVVM patterns intermingled across the Toolkits in this fashion. This approach also seems to mix a lot more of the View and the ViewModel paradigms together breaking the notion of what MVVM is supposed to represent. And wouldn't this make it harder to localize the UI properties? |
Beta Was this translation helpful? Give feedback.
-
No, because dataannotation attributes already have support for localization and strings resources.. This aproach dont mix the things. Actualy we have to descrieve propertyes in View In other words we need to make tuo times the same thing ! with this aproach we only make the model and this is an extension, the user can adopt this metodology or not but think with me, if you have to put in xaml and in viewmodel [Display(DisplayName="Name of user", Description = "Type here the name of user")] you have the same information in tuo places and if you change something in xaml like It is bether tu put all information only in annotations of view model |
Beta Was this translation helpful? Give feedback.
-
This seems an interesting idea but the vagueness of the outline and partial code samples leave lots of open questions. For starters:
|
Beta Was this translation helpful? Give feedback.
-
As of right now, this proposal is not actionable. If new APIs are being proposed, there should be an actual API breakdown that can be reviewed. Right now I don't really understand what APIs are actually being proposed, how they would work, or how they would be used 🙂 |
Beta Was this translation helpful? Give feedback.
-
the api is «anycontrol be:MVVM.PropertyName="PhoneNumber" be:MVVM.ViewModel="vm" /» and the infrastructure to make all things automatically binded from view model, independent of type of the control And this dont break any existing API or modify XAML. It is an Upgrade like x:Bind upgraded x:Binding |
Beta Was this translation helpful? Give feedback.
-
MVVM is only a pleasant sugar name, can be other name like be:AnnotationBinder
Yes can be, but I will prefer to donate it to Microsoft in advance of community and for broader audience
Yes, It have no dependencies to other things than ComponentModel.DataAnotations Attributes and can easily integrated on any Model View Framework
I will provide an sample. The difference is that you don't put anything in XAML other than the property name of binded item in XAML and all other information of this property is readed from Attributes of this property from ViewModel. Things like Header, PlaceHolder, Tooltip, Mask, Validation (error messages), InputScope (from DataType), I Say error messages ? they are binded from ObservableValidator class viewlmodel to Description of controls that have it and to other TextBlocks controls when not have it. And a lot of other things, all with only a 2 lines of XAML code.
The impact is the same of using Binding versus x:Bind but it is the first version. When the framework is stabilezed we can make an compiler version like source generators that Modify XAML before compilation and make things statically and not at runtime.
All checks are compiled in Attributes in ViewModel statically.
The same way you use localization in AttributesValidators, using resources and language
Very Extensible, you cant configure the way of binder manipulate new validators and new attributes to Binder at startup application but it is a first version, a lot of things ca be possible in future.
be:MVVM works with any attributes, including custom user attributes
The same way it works with normal controls, it use the bind infrastructure and ComponentModel.DataAnottions, works on any level of XAML, including Page, Normal Controls, Custom Controls, User Controls, etc
It is Limeted only to bind the main property of knowed controls (Text for TextBox, SelectedValue for Listbox, etc), But this can be user configured and if not configured by user, be:MVVM will take care of all other properties and the user can bind main property in old way. Anyway we dont wate time coding lot of tedius code
If you made view model and XAML indenpendly, you write 2 times. How can we dont neeed to be done ? Please explain me. |
Beta Was this translation helpful? Give feedback.
-
I feel like I should repeat this: as it currently stands, this issue is not actionable, as this is (presumably) meant to be an API proposal, but there is no API being proposed. As I mentioned, this needs to have an API breakdown so we can actually see the proposed API surface you'd like to contribute. As a reference, you can take a look at these issues: CommunityToolkit/WindowsCommunityToolkit#4300, CommunityToolkit/WindowsCommunityToolkit#3530. I'm also quite worried about the fact this proposed feature, as far as I can tell, will heavily rely on reflection, and I don't think that's the direction we should be moving towards. It might be acceptable for cases where viewmodels use validation, since that's already not trimming friendly, but not in general cases where viewmodels just use MVVM without validation. |
Beta Was this translation helpful? Give feedback.
-
@cesarchefinho do you have your code in an existing repository somewhere that folks could look at or try directly? |
Beta Was this translation helpful? Give feedback.
-
Dont Worried about reflection, this is the first version and all reflections that today is made in runtime can be eliminated and made at compile time to generate non reflection code that is statically compiled. And this extension is an option to user, the user can use this extension to code fast when is appropriate or user can do thinks in the traditional way... |
Beta Was this translation helpful? Give feedback.
-
if you authorize i can post a code here |
Beta Was this translation helpful? Give feedback.
-
There is nothing to authorize, you're free to post code you wrote wherever you want, you own that. |
Beta Was this translation helpful? Give feedback.
-
@cesarchefinho why don't you create your own repository on your own account first to show more about how your idea works with a sample/example project? There's a lot of gaps here that we're not able to follow based on your brief textual descriptions. |
Beta Was this translation helpful? Give feedback.
-
this is the complete first version implementation of binding extensions and an sample forrm and view model |
Beta Was this translation helpful? Give feedback.
-
i made an repository https://github.com/cesarchefinho/MVVMBindingExtensions |
Beta Was this translation helpful? Give feedback.
-
Thanks @cesarchefinho for the repo, note that the Toolkit is licensed under an MIT license, I noticed you made your repo GPL 3.0. Do you mind changing it? Makes it easier for us to look at or consume later if something were to happen to you, or someone else wants to pick it up and contribute or help you contribute it to the Toolkit or another .NET or .NET Foundation project later (as the majority of them use MIT or Apache 2). |
Beta Was this translation helpful? Give feedback.
-
changed to MIT license. Thanks. |
Beta Was this translation helpful? Give feedback.
-
If you have any comments or suggestions for me about this extensions, please make it. I also don't have the knowledge to implement an compiler time version of this without reflection. If you have any information about xaml compilation phase or if you want to contribute with coding, please tell me. Thanks |
Beta Was this translation helpful? Give feedback.
-
Describe the problem
XAML is too verbose and cant read from anotations attibutes like validatos, data type etc in ViewModel
also XAML does not have and Validation Error mecanism for individual control error messages binded to ViewModel
Describe the solution
Binding Extensions MVVM Proposal to WINUI3 and UWP
An Extension to XAML language that simplify markup and expand XAML capabilities
by automatcaly bind control to ViewModel,
display validation error messages individualy per control,
and read all possible ViewModel Annotations to control without any coding
things like this
simple will be this in markup:
the be:MVM extensions make all binding to you based on anottations of view model
also the be.MVVM extension use ObservableValidator and ObservablePropertyes
also the be:MVVM resolve binding to indexed string array attribute of collumns errors (bug in winui3 workedarrounded by be:MVVM)
also suport display error messages in TextBlock other than description property for controls that dont have Description
example:
Suported controls
**IT IS WORKING AND IS FUNCTIONAL
Also works on UNO in All Platforms AND in WINUI3 and UWP**
Alternatives
Waste a lot of time
Additional info
It is already implemented, i want to contribute to Microsoft
Help us help you
Yes, I'd like to be assigned to work on this item.
Beta Was this translation helpful? Give feedback.
All reactions