将整个Google地图属性绑定,以便代码后台可以添加 – C# .Net Maui

huangapple go评论88阅读模式
英文:

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:

&lt;ContentPage xmlns=&quot;http://schemas.microsoft.com/dotnet/2021/maui&quot;
             xmlns:x=&quot;http://schemas.microsoft.com/winfx/2009/xaml&quot;
             xmlns:local=&quot;clr-namespace:CellularSignal1&quot;
             xmlns:maps=&quot;clr-namespace:Microsoft.Maui.Controls.Maps;assembly=Microsoft.Maui.Controls.Maps&quot;
             x:Class=&quot;CellularSignal1.Mapper&quot;
             x:DataType=&quot;local:Mapper&quot;
             x:Name=&quot;Mapper&quot;
             Title=&quot;Mapper&quot;&gt;
    &lt;maps:Map x:Name=&quot;map&quot;/&gt;
    &lt;!--Would like something like...
    &lt;maps:Map Reference=&quot;{Binding=googleMaps}&quot;/&gt;
    --&gt;
&lt;/ContentPage&gt;

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 中:

&lt;ContentPage xmlns=&quot;http://schemas.microsoft.com/dotnet/2021/maui&quot;
             xmlns:x=&quot;http://schemas.microsoft.com/winfx/2009/xaml&quot;
             xmlns:local=&quot;clr-namespace:CellularSignal1&quot;
             xmlns:maps=&quot;clr-namespace:Microsoft.Maui.Controls.Maps;assembly=Microsoft.Maui.Controls.Maps&quot;
             x:Class=&quot;CellularSignal1.Mapper&quot;
             x:DataType=&quot;local:Mapper&quot;
             x:Name=&quot;Mapper&quot;
             Title=&quot;Mapper&quot;&gt;
&lt;/ContentPage&gt;

在代码后台:

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:

&lt;ContentPage xmlns=&quot;http://schemas.microsoft.com/dotnet/2021/maui&quot;
             xmlns:x=&quot;http://schemas.microsoft.com/winfx/2009/xaml&quot;
             xmlns:local=&quot;clr-namespace:CellularSignal1&quot;
             xmlns:maps=&quot;clr-namespace:Microsoft.Maui.Controls.Maps;assembly=Microsoft.Maui.Controls.Maps&quot;
             x:Class=&quot;CellularSignal1.Mapper&quot;
             x:DataType=&quot;local:Mapper&quot;
             x:Name=&quot;Mapper&quot;
             Title=&quot;Mapper&quot;&gt;
&lt;/ContentPage&gt;

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;
     }
}

huangapple
  • 本文由 发表于 2023年3月23日 09:24:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/75818541.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定