question

Gas-9611 avatar image
0 Votes"
Gas-9611 asked Gas-9611 answered

How to Bind XML to Object in ViewModel with prism.

This should be a one line solution but I've been at it for days.
Very simple beginner problem....

My Viewmodel has an Part object with Properties. eg. color etc.

  private PartsExt _Part;
         public PartsExt Part
         {
             get => _Part;
             set => SetProperty(ref _Part, value);
         }


My ViewModel Constructor is

   public PartDetailsViewModel(INavigationService navigationService) : base (navigationService)
         {
         }


I get my Part no problem here:

   public interface IInitialize
         {
             void Initialize(INavigationParameters parameters);
         }
    
    
         public override async void Initialize(INavigationParameters parameters)
         {
             if (parameters.ContainsKey("SelectedPart"))
             {
                 Part= parameters.GetValue<PartsExt>("SelectedPart");
             }

            }

Question
How do I bind to the XAML (remember Prism has AutowireViewModel)
I would have thought:
Text="{Binding Part.color}"

Then thought:

    xmlns:local2="clr-namespace:iBasisAppV1.ViewModels;assembly=iBasisAppV1"
    
    
 <ContentPage.BindingContext>
         <local2:PartDetailsViewModel />
     </ContentPage.BindingContext>


But that gives error:

Type 'PartDetailsViewModel' is not usable as an object element because it is not public or does not define a public parameterless constructor or a type converter.


Next trial was setting BindingContect of Outermost StackLayout
e.g. BindingContext="{Binding Part}" and label = Text="{Binding color}"

 But that gives error 'Part' property not found on 'iBasisAppV1.Models.PartsExt', target property: 'Xamarin.Forms.Label.BindingContext'


I know the answers simple but with Prism involved, its impossible to find answers.










dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

Gas-9611 avatar image
0 Votes"
Gas-9611 answered

This make zero sense but I'll leave it here.



The Object name has to be the same name as the Object type name.


  private PartsExt _PartsExt;
         public PartsExt PartsExt
         {
             get => _PartsExt;
             set => SetProperty(ref _PartsExt, value);
         }

then
Text="{Binding PartsExt.Description}" works.


Xamarins a disaster, full of magic 'words' and 'spells' nobody can possibly know!

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.