Image source not binding in xamarin form - c#

My object has a property set elsewhere called:
ImageSrc = "icon.png"
In xaml file I am trying to bind to this:
<Image Source="{Binding ImageSrc}" />
In the ViewModel I am setting the property as such:
private string _imageSrc;
public string ImageSrc
{
get => this._imageSrc;
set
{
this._imageSrc = value;
this.RaisePropertyChanged(() => this.ImageSrc);
}
}
public override Task InitializeAsync(object _params)
{
ObjectParameters _objectParameters = (ObjectParameters)_params;
this.ImageSrc = _objectParameters.ImageSrc;
return Task.CompletedTask;
}
If I set this in class constructor:
this.ImageSrc = "icon.png";
it binds fine but not after calling:
public override Task InitializeAsync(object _params)
the property is set properly with the correct value of "icon.png" but the image does not show like when set in the class constructor. Any ideas appreciated.

I had to right click on Resources/drawable/icon.png then Add To Project and make sure Build Action: AndroidResource

Related

Bind WebView2 to ViewModel Using Caliburn Micro

I am using the guide located her for reference: https://learn.microsoft.com/en-us/microsoft-edge/webview2/gettingstarted/wpf
Utilizing that guide, I was able to get WebView2 started within my application. Now I am trying to seperate the code into a ViewModel as I will have many more elements on that page. The application as a whole uses Caliburn Micro. I am able to bind everything to the ViewModel except for the WebView2 itself. Wheneer I Select the Go button it states that the WebView is null. I tried manually setting the WebView however that does not work.
BrowserView.xaml:
<Button
x:Name="ButtonGo"
Content="Go"
/>
<TextBox x:Name = "Addressbar"
/>
<wv2:WebView2 x:Name = "WebView"
Source="{Binding WebViewSource}"
/>
BrowserViewModel.cs
private WebView2 _webView;
public WebView2 WebView
{
get
{
return _webView;
}
set
{
_webView = value;
NotifyOfPropertyChange(() => WebView);
}
}
public string WebViewSource { get; set; } = "http://Google.com";
private string _addressbar;
public string Addressbar
{
get
{
return _addressbar;
}
set
{
_addressbar = value;
NotifyOfPropertyChange(() => Addressbar);
}
}
public void ButtonGo()
{
if (WebView != null && WebView.CoreWebView2 != null)
{
WebView.CoreWebView2.Navigate("https://bing.com");
}
}
No matter what I try WebView keeps coming back null and I am not able to change the page.
As aepot commented, removing the Webview property and notifying the change in the source resolved the issue. The code now looks like this for the view:
<Button x:Name="ButtonGo"
Content="Go"/>
<TextBox x:Name = "Addressbar"/>
<wv2:WebView2 x:Name = "WebView"
Source="{Binding WebViewSource}"/>
And this for the ViewModel:
public string Addressbar
{
get
{
return _addressbar;
}
set
{
_addressbar = value;
NotifyOfPropertyChange(() => Addressbar);
}
}
public void ButtonGo()
{
WebViewSource = Addressbar;
NotifyOfPropertyChange(() => WebViewSource);
}
A view model is not supposed to keep a reference to an element like WebView2. This is not MVVM and not how Caliburn.Micro works. A view model defines the source properties.
If you add a TextBlock property (you should not!) to the view model, it will also always be null just like your WebView2 property is, even if you add a TextBlock with a corresponding name in the XAML markup.
I am afraid this doesn't make much sense, both regarding MVVM in general and Caliburn.Micro in particular.

Property not being set after model is updated

My application is using MVVM and exposes the properties on the model by using getter properties on the view model. The problem I am having is when I set one of my properties on the model, the property on the view model that gets the property from the model is not being updated - the get/set is never called after the property on the model is set. If I breakpoint on the ColourR of the view model, neither the get or set is hit after SelectedColour is set even though ColourR on the model is being set.
The ColourR property on the model is set by the view model when SelectedColour on the view model updates the property on the model. The text box on my view is binded to the ColourR property on the view model, which should get the value of the property on the model, but whenever this value changes it is not updating correctly.
If I bind the textbox directly to the model using {Binding LineAppearanceLayerDefinition.ColourR}, it works correctly but I would like to understand why the binding does not work with my property on the view model as I would prefer to bind this way.
Model:
public class AppearanceLayerDefinition : BindableBase
{
private string _colourR;
public string ColourR
{
get { return _colourR; }
set
{
SetProperty(ref _colourR, value);
}
}
private Color _selectedColour;
public Color SelectedColour
{
get { return _selectedColour; }
set
{
SetProperty(ref _selectedColour, value);
ColourR = value.R.ToString();
}
}
}
View Model:
public class AppearanceLayerDefinitionLineViewModel
{
public AppearanceLayerDefinitionLineViewModel(ApperanceLayerDefinition appearanceLayerDefinition)
{
LineAppearanceLayerDefinition = appearanceLayerDefinition;
}
public LineAppearanceLayerDefinition { get; private set; }
public Color SelectedColour
{
get { return LineAppearanceLayerDefinition.SelectedColour; }
set
{
LineAppearanceLayerDefinition.SelectedColour = value;
}
}
private string _colourR;
public string ColourR
{
get { return LineAppearanceLayerDefinition.ColourR; }
set
{
LineAppearanceLayerDefinition.ColourR = value;
SetProperty(ref _colourR, value);
}
}
}
XAML:
<TextBox Grid.Column="2" Style="{StaticResource AppearanceLayersTextBoxGroupStyle}" Text="{Binding ColourR}" />

Property Change not being updated in the View dynamically

My binding is not updating the View in real-time when an OnPropertyChanged is called. I am able to set breakpoints to see the value being changed in the View Model. The View eventually updates when I make another selection, but not in real-time. I believe I know what the problem is, but I am struggling with fixing the problem. I think the problem is that I am calling the CreateChartNode method that is creating a new instance.
In my View Model, it looks like this:
public class ChartObjectVM : INotifyPropertyChanged
public ChartNode
{
get
{
return CreateChartNode();
}
}
private string fullText;
public string FullText
{
get
{
return this.fullText;
}
set
{
this.fullText = value;
OnPropertyChanged("FullText");
}
public ChartNode CreateChartNode()
{
ChartNode newChartNode = new ChartNode();
And in my ViewModel I am doing:
public void CreateNode()
{
ChartObjectVM cObjectVM = new ChartObjectVM();
ChartNode = cObjectVM.CreateNode();
}
My binding in the View looks like:
{Binding Path = SelectedChartObject.UserObject.FullText, UpdatedSourceTrigger=PropertyChange, Mode=TwoWay}
Where SelectedChartObject is the currently selected ChartObjectVM.
Like I have said, I am pretty sure the problem is the CreateNode() method being called. I think there are two instances under the hood but I can't figure out why.

how to use parameter in viewmodel ins xamarin.forms

Im trying to pass parameter to viewmodel in Xamarin.Forms.But Also I want to use it while loading the page ,to call a rest service with that parameter.But In load event parameter is null always.(I can see that parameter if I bind it to a label or etc...)
How can I make it work properly and use that parameter inside the load event of viewmodel? Any help is appreciated,thanks
here is the button click event and navigate to Samplepage.
private void OntaptedBildirimItem(string param)
{
this._navigationService.NavigateTo(nameof(Views.SamplePage), param);
}
this is the Sample Page Viewmodel
private string _id;
public string id
{
get
{
return _id;
}
set
{
Set(() => id, ref _id, value);
}
}
public SamplePageViewModel(INavigationService navigationService) : base(navigationService)
{
this._navigationService = navigationService;
this.Title = "Sample Page=" + id; // here the id is always null,but if I use it to bind a label in xaml ,it has a value.
}
This is the SamplePage Code
public SamplePage(string parameter)
{
InitializeComponent();
var viewModel = this.BindingContext as ViewModels.SamplePageViewModel;
if(viewModel != null && parameter != null)
{
viewModel.bildirimid = parameter;
}
}
Thx for this Miguel ,this helps me a lot ,I can do what I want by this approach .You can send parameter while navigating a page in xamarin.forms like this
_navigationService.NavigateToAsync<SamplePageViewModel>("Your_parameter");
and you can get that parameter in that page's viewmodel like this
public override async Task InitializeAsync(object yourparameter)
{
// use yourparameter as you wish
}
We need see NavigationService .
You should add bindingContext in NavigationService not in SamplePage.
In This Sample uses NavigationService with a parameter and a better NavigationService structure. You should see it.
Send feedback please.

Binding combobox - WPF & C#

I have read loads of posts about this topic, but I cannot for the life of me figure this out, so your help is appreciated as I am losing the will to live!
I am trying to bind a list to a combobox in WPF, here is my code:
ViewModel:
public class ViewModelAddRegion
{
public List<DataAccessLayer.Model.CountryList> CountryList { get; set; }
public object GetCountryList()
{
List<DataAccessLayer.Model.CountryList> CountryList = new List<DataAccessLayer.Model.CountryList>();
CountryList = Data.DatabaseGets.GetAllCountries();
return CountryList;
}
}
So that gets my list. In the backing to my window, the code is:
public AddRegion()
{
var vm = new WineDatabase.ViewModel.ViewModelAddRegion();
var CountryAllList = vm.GetCountryList();
DataContext = CountryAllList;
InitializeComponent();
}
And finally, in my window:
<ComboBox Name="CountryList"
Margin="159,0,-160,0"
Grid.Row="1"
Grid.RowSpan="2"
ItemsSource="{Binding CountryAllList}"
DisplayMemberPath="CountryName"/>
Debugging, my list is populated as expected, but the combobox is forever empty.
Thanks for any assistance at all!
CountryAllList is just a local variable that you can't bind to. See the Data Binding Overview article on MSDN for details.
You should assign the ViewModel instance to the DataContext
var vm = new WineDatabase.ViewModel.ViewModelAddRegion();
vm.CountryList = vm.GetCountryList();
DataContext = vm;
and bind to its CountryList property
<ComboBox ItemsSource="{Binding CountryList}" ... />
Finally, in your GetCountryList method, it doesn't make much sense to assign the return value of Data.DatabaseGets.GetAllCountries() to a local variable. You could instead directly return it from the method.
public List<DataAccessLayer.Model.CountryList> GetCountryList()
{
return Data.DatabaseGets.GetAllCountries();
}
The GetCountryList() method may as well directly assign to the CountryList property
public void GetCountryList()
{
CountryList = Data.DatabaseGets.GetAllCountries();
}
and you could write the initialization code like this:
var vm = new WineDatabase.ViewModel.ViewModelAddRegion();
vm.GetCountryList();
DataContext = vm;
Change AddRegion method to:
public AddRegion()
{
var vm = new WineDatabase.ViewModel.ViewModelAddRegion();
vm.CountryList = vm.GetCountryList();
DataContext = vm;
InitializeComponent();
}
And in ComboBox set ItemsSource="{Binding CountryList}"

Categories