英文:
Bind whole property of google maps so codebehind can add - C# .Net Maui
问题
I'm looking for a way to create a google map in the codebehind of my app, and bind to the codebehind map in xaml.
This is my Mapper.xaml currently, but I would like a way to bind to the whole property of the map to be referenced in codebehind. It's currently creating the property in xaml:
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:CellularSignal1"
xmlns:maps="clr-namespace:Microsoft.Maui.Controls.Maps;assembly=Microsoft.Maui.Controls.Maps"
x:Class="CellularSignal1.Mapper"
x:DataType="local:Mapper"
x:Name="Mapper"
Title="Mapper">
<maps:Map x:Name="map"/>
<!--Would like something like...
<maps:Map Reference="{Binding=googleMaps}"/>
-->
</ContentPage>
Meaning, don't create a new property in xaml, just reference to a property in codebehind. If I could do something like this I would then have in my codebehind...
using Microsoft.Maui.Controls.Maps;
public partial class Mapper : ContentPage
{
Map googleMapper;
public Mapper()
{
   Location location = new Location(36.9628066, -122.0194722);
   MapSpan mapSpan = new MapSpan(location, 0.01, 0.01);
   googleMapper = new Map(mapSpan);
   InitializeComponent;
   // Add a bunch of other stuff to map.
   addPins();
 }
}
the reason I want to do something like this in the first place is that I have used what it is currently, and then referenced the map, and moved to region upon startup, but there is a delay in moving to the new region vs what the default location is. I'm trying to see if I can remove this delay by initializing the map with the location to go to.
英文:
I'm looking for a way to create a google map in the codebehind of my app, and bind to the codebehind map in xaml.
This is my Mapper.xaml currently, but I would like a way to bind to the whole property of the map to be referenced in codebehind. It's currently creating the property in xaml:
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:CellularSignal1"
             xmlns:maps="clr-namespace:Microsoft.Maui.Controls.Maps;assembly=Microsoft.Maui.Controls.Maps"
             x:Class="CellularSignal1.Mapper"
             x:DataType="local:Mapper"
             x:Name="Mapper"
             Title="Mapper">
    <maps:Map x:Name="map"/>
    <!--Would like something like...
    <maps:Map Reference="{Binding=googleMaps}"/>
    -->
</ContentPage>
Meaning, don't create a new property in xaml, just reference to a property in codebehind. If I could do something like this I would then have in my codebehind...
using Microsoft.Maui.Controls.Maps;
public partial class Mapper : ContentPage
{
    Map googleMapper;
    public Mapper()
    {
       Location location = new Location(36.9628066, -122.0194722);
       MapSpan mapSpan = new MapSpan(location, 0.01, 0.01);
       googleMapper = new Map(mapSpan);
       InitializeComponent;
       // Add a bunch of other stuff to map.
       addPins();
     }
}
the reason I want to do something like this in the first place is that I have used what it is currently, and then referenced the map, and moved to region upon startup, but there is a delay in moving to the new region vs what the default location is. I'm trying to see if I can remove this delay by initializing the map with the location to go to.
答案1
得分: 1
在 xaml 中:
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:CellularSignal1"
             xmlns:maps="clr-namespace:Microsoft.Maui.Controls.Maps;assembly=Microsoft.Maui.Controls.Maps"
             x:Class="CellularSignal1.Mapper"
             x:DataType="local:Mapper"
             x:Name="Mapper"
             Title="Mapper">
</ContentPage>
在代码后台:
using Microsoft.Maui.Controls.Maps;
public partial class Mapper : ContentPage
{
    Map googleMapper;
    public Mapper()
    {
       Location location = new Location(36.9628066, -122.0194722);
       MapSpan mapSpan = new MapSpan(location, 0.01, 0.01);
       googleMapper = new Map(mapSpan);
       InitializeComponent;
       // Add a bunch of other stuff to map.
       addPins();
      
      this.Content = googleMapper;
     }
}
英文:
You can add the Map in the code behind such as:
In the xaml:
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:CellularSignal1"
             xmlns:maps="clr-namespace:Microsoft.Maui.Controls.Maps;assembly=Microsoft.Maui.Controls.Maps"
             x:Class="CellularSignal1.Mapper"
             x:DataType="local:Mapper"
             x:Name="Mapper"
             Title="Mapper">
</ContentPage>
In the code behind:
using Microsoft.Maui.Controls.Maps;
public partial class Mapper : ContentPage
{
    Map googleMapper;
    public Mapper()
    {
       Location location = new Location(36.9628066, -122.0194722);
       MapSpan mapSpan = new MapSpan(location, 0.01, 0.01);
       googleMapper = new Map(mapSpan);
       InitializeComponent;
       // Add a bunch of other stuff to map.
       addPins();
      
      this.Content = googleMapper;
     }
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论