【问题标题】:Xamarin - Binding to a static class in a ControlTemplateXamarin - 绑定到 ControlTemplate 中的静态类
【发布时间】:2017-06-16 05:39:18
【问题描述】:

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

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

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

我继续收到以下错误:

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

【问题讨论】:

  • 澄清一下——这不是一个真正的静态类,而是一个静态类的实例。我也尝试绑定到一个静态类,但是我无法从 INotifyPropertyChanged 继承,所以这似乎不是要走的路。

标签: c# xamarin binding xamarin.forms controltemplate


【解决方案1】:

我使用此处的示例进行了此操作:Binding to a property within a static class instance

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

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

“路径”似乎是最大的不同。我想我会在它刚刚弹出时再阅读一些内容。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-02-10
    • 2014-09-10
    • 2014-01-29
    • 1970-01-01
    • 2012-10-15
    • 2013-08-17
    • 1970-01-01
    相关资源
    最近更新 更多