How to make my Mapsui layers visible and rendered after I call the below void function from another class? MyMap.Refesh() does so I don’t get a null error?
In my code behind MapsuiControl.xaml.cs file I have:
public void CreateImageLayer()
{
MyMap.Map.Layers.Add(_imageLayerFactoryTwo.CreateLayer());
MyMap.Refresh();
}
In the xaml file MapsuiControl.xaml I have:
<winui:MapControl x:Name="MyMap" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" />
The another c# class call the code-behind file in this function:
private void SendImageLayerString()
{
mapsuiControl.CreateImageLayer();
}
I can only show the layer when I using this code in the constructor at the code-behind file MapsuiControl.xaml.cs but I want it in a void function that is called from another c# class: MyMap.Map.Layers.Add(_imageLayerFactoryTwo.CreateLayer());
The whole constructor when I can show the datalayer is:
public MapsuiControl()
{
this.InitializeComponent();
MyMap.Map.Layers.Add(OpenStreetMap.CreateTileLayer());
MyMap.Map.Layers.Add(_imageLayerFactory.CreateLayer());
}
The c# class I want to call it from:
public class UserSenderViewModel : ObservableRecipient
{
private MapsuiControl mapControl; /*= new ();*/
public UserSenderViewModel()
{
mapControl = new MapsuiControl();
}
public void SendUserMessage()
{
mapControl.CreateImageLayer();
}
Another example:
I had also tried to create a MapsuiControl from the beginning like MainViewModel.cs => UserSenderViewModel
public partial class MainViewModel : ObservableObject
{
private MapsuiControl _mapsuiControl;
private UserSenderViewModel _userSenderViewModel;
public MainViewModel()
{
_mapsuiControl = new MapsuiControl();
_userSenderViewModel = new UserSenderViewModel(_mapsuiControl);
}
public UserSenderViewModel UserSenderViewModel { get { return _userSenderViewModel; } }
and UserSenderViewModel.cs:
public class UserSenderViewModel : ObservableRecipient
{
private MapsuiControl _mapControl;
public UserSenderViewModel(MapsuiControl mapControl)
{
_mapControl = mapControl;
}
public void SendUserMessage()
{
_mapControl.CreateImageLayer();
}
With ICommand:
MainViewModel.cs:
public ICommand SendImageLayerCommand { get; }
private string imageLayer = "Image Layer";
public string ImageLayer
{
get => imageLayer;
private set => SetProperty(ref imageLayer, value);
}
private void SendImageLayerString()
{
_mapsuiControl.CreateImageLayer();
}
Xaml file in a usercontrol:
<Button
Content="Show Image Datalayer"
Command="{x:Bind ViewModel.SendImageLayerCommand, Mode=OneWay}"
/>