从Xaml.cs文件访问MainActivity的方法

3zwtqj6y  于 2023-02-27  发布在  其他
关注(0)|答案(1)|浏览(167)

以下问题:在我的Xamarin:Forms应用程序中,我需要调用一个方法来启动位于我的MainActivity中的广播消息。OnButtonClicked()方法位于我的xaml.cs文件中
我的主要活动编码

public class MainActivity : 
    global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
    public static MainActivity MainActivityInstance { get; private set; }
    Receiver rec;
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        global::Xamarin.Forms.Forms.Init(this, savedInstanceState);

        MainActivityInstance = this;
        rec = new Receiver();
        LoadApplication(new App());
    }
    public override void OnRequestPermissionsResult(int requestCode, string[] 
        permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
    {
        Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, 
        grantResults);

        base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    public void sendBroadcastMessage()
    {
        if (rec != null)
        {
            SendBroadcast(new Intent("com.xamarin.example.TEST"));
        }
        else
        {
            return;
        }
    }

    protected override void OnResume()
    {
        base.OnResume();
        RegisterReceiver(rec, new IntentFilter("com.xamarin.example.TEST"));
        // Code omitted for clarity
    }

    protected override void OnPause()
    {
        UnregisterReceiver(rec);
        // Code omitted for clarity
        base.OnPause();
    }

}

}
我的广播接收者:

namespace AppCommunication.Droid
{
[BroadcastReceiver]
public class Receiver : BroadcastReceiver
{
    public override void OnReceive(Context context, Intent intent)
    {
        Toast.MakeText(context, "Received intent!", ToastLength.Short).Show();
    }
}

}
我的XAML文件:

<?xml version="1.0" encoding="utf-8" ?>
    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="AppCommunication.MainPage">

    <StackLayout>
        <Button Text="START"
         Clicked="OnStartButtonClicked">
        </Button>
    </StackLayout>

    </ContentPage>

xaml.cs文件

namespace AppCommunication
{
public partial class MainPage : ContentPage
{
    
    public MainPage()
    {
        InitializeComponent();
    }

    private void OnStartButtonClicked(object sender, EventArgs e)
    {
        //Do something
    }
}

}
我需要从我的xaml.cs文件的OnStartButtonClicked方法中的MainActivity调用SendBroadcast()方法。正如您在MainActivity的代码中所看到的,我已经尝试使用静态MainActivity对象,但即使如此,MainActivity在xaml.cs文件中也不可见。问题是什么。请帮助:-(
谢谢

3hvapo4f

3hvapo4f1#

直接从Forms项目调用Android代码是不可能的,在这种情况下,我将使用内置的DependencyService。
依赖关系服务简介:https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/dependency-service/introduction
示例:在Forms项目中,创建一个接口,声明特定于平台的类应执行的操作:

namespace AppCommunication
{
    public interface IBroadcastService
    {
        void SendBroadcast();
    }
}

在您的Android项目中,使用接口并向DependencyService注册依赖项

[assembly: Xamarin.Forms.Dependency(typeof(BroadcastService))]
namespace AppCommunication.Droid
{
    public class BroadcastService: IBroadcastService
    {
        public void SendBroadcast()
        {
             //Add broadcast sending logic
        }
    }
}

现在可以按如下方式调用SendBroadcast方法:

namespace AppCommunication
{
     public partial class MainPage : ContentPage
     {
            
         public MainPage()
         {
             InitializeComponent();
         }
        
         private void OnStartButtonClicked(object sender, EventArgs e)
         {
             Xamarin.Forms.DependencyService.Get<IBroadcastService>().SendBroadcast();
         }
    }
}

相关问题