c# - Xamarin - 绑定(bind)到 ControlTemplate 中的静态类

标签 c# xamarin binding xamarin.forms controltemplate

我正在构建一个 Xamarin.Forms 移动应用程序,该应用程序将在用户使用该应用程序时播放音频文件。用户将能够在开始播放文件后继续使用该应用程序,文件将通过静态类播放/管理,而不是单个 View (因为该 View 可能会在文件播放时从导航堆栈中删除仍在播放)。

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

现在我只有一个开关,其中 IsToggled 应该绑定(bind)到可绑定(bind)属性 IsPlaying 绑定(bind)到静态类的可绑定(bind)属性。

我继续收到以下错误:

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

这是我的静态类:

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 中绑定(bind)的新手,但我正在阅读的所有内容似乎更适用于 ContentPages,而且它与 ControlTemplates 的工作方式不同。

最佳答案

我使用此处的示例实现了此功能:Binding to a property within a static class instance

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

我确实更改了我的类以匹配上面的答案,但最大的变化是绑定(bind)代码在 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}" />

“路径”似乎是最大的区别。我想我会在它刚刚弹出时阅读更多内容。

关于c# - Xamarin - 绑定(bind)到 ControlTemplate 中的静态类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41946500/

相关文章:

c# - Xamarin-在Gridview中创建一个额外的列,其垂直跨度为2

Xamarin.Forms - 如何实现 45 度。倾斜背景颜色

c# - Xamarin Android 应用程序在 Release模式下崩溃(Parse.Android SDK)

java - FXML:将 TableViews 项目属性绑定(bind)到 Controller

wpf - 自定义图片框中的图像不刷新

c# - 如何创建不需要管理员访问权限的 Windows 安装程序 MSI

c# - 如何使用 FFMpeg 转码数据流 (C#)

c# - C++ 等效于 C# Yield?

C# 属性\避免硬编码值

c# - 以编程方式创建 ListView 项的绑定(bind)