Xamarin - 绑定到 ControlTemplate 中的静态 class

Xamarin - Binding to a static class in a ControlTemplate

我正在构建一个 Xamarin.Forms 移动应用程序,它将在用户使用该应用程序时播放音频文件。用户将能够在开始播放文件后继续使用该应用程序,并且该文件将通过静态 class,而不是单个视图 played/managed(因为该视图可能会从仍在播放文件时的导航堆栈)。

我想要 mini-player 在应用程序内的任何其他视图中可见,我正在使用 ControlTemplate 来完成此操作。我希望这个控件模板有一些项目绑定到静态 class 中的属性(例如播放器状态 [playing/paused]、剩余时间、标题等)以及控制运行 静态方法 class。我可以 运行 在我的 app.xaml 页面(ControlTemplate 所在的位置)中使用隐藏代码的方法,但是我很难绑定我的可绑定属性。

现在我只有一个开关,其中 IsToggled 应该绑定到可绑定 属性 IsPlaying 绑定到静态 class' 可绑定 属性.

我继续收到以下错误:

Xamarin.Forms.Xaml.XamlParseException: Type AudioPlayer.Current not found in 
xmlns clr-namespace:AudioPlayerBar;assembly=AudioPlayerBar...

我已经在我所有的 .xaml 中的 XMLNS 中定义了名称空间,所以我不确定发生了什么。我的整个项目都在 GitHub https://github.com/ChetCromer/XamarinPrototypes/tree/master/AudioPlayerBar

这是我的静态 class:

using System;
using System.ComponentModel;
using Xamarin.Forms;

namespace AudioPlayerBar
{
public  class AudioPlayer :INotifyPropertyChanged
{
    // Singleton for use throughout the app
    public static AudioPlayer Current = new AudioPlayer();

    //Don't allow creation of the class elsewhere in the app.
    private AudioPlayer()
    {
    }

    private bool _IsPlaying = false;

    //property for whether a file is being played or not
    public bool IsPlaying
    {
        get
        {
            return _IsPlaying;
        }
        set
        {
            _IsPlaying = value;
            OnPropertyChanged("IsPlaying");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged == null)
            return;

        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}
}

这是我的 ControlTemplate(在 app.xaml 内)

<ControlTemplate x:Key="PlayerPageTemplate">
            <StackLayout VerticalOptions="FillAndExpand">
                <ContentView VerticalOptions="FillAndExpand">
                    <ContentPresenter />
                </ContentView>
                <StackLayout x:Name="stackFooterContent">
                    <Label Text="IsPlaying Value:"/>
                    <Switch IsToggled="{x:Static local:AudioPlayer.Current.IsPlaying}" />
                    <Button Text="Toggle IsPlaying" Clicked="Click_PlayPause" />
                </StackLayout>
            </StackLayout>
        </ControlTemplate>

有什么想法吗?我承认我是 Xamarin 中绑定的新手,但我正在阅读的所有内容似乎更适用于 ContentPages,而且它与 ControlTemplates 的工作方式不同。

我使用此处的示例完成了此工作:Binding to a property within a static class instance

这不是同一个问题,所以我留下我的问题并回答它。

我确实更改了 class 以匹配上面的答案,但最大的变化是绑定代码在 xaml 中的外观:

//New 2 way bindable property I made up
<Label Text="{Binding Source={x:Static local:AudioPlayer.Instance},Path=Title}"/>

//Boolean bindable property
<Switch IsToggled="{Binding Source={x:Static local:AudioPlayer.Instance},Path=IsPlaying}" />

"Path" 似乎是最大的不同。我想我会在它刚刚弹出时再多读一些。