Xamarin-绑定到ControlTemplate中的静态类 [英] Xamarin - Binding to a static class in a ControlTemplate

查看:67
本文介绍了Xamarin-绑定到ControlTemplate中的静态类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm building a Xamarin.Forms mobile app that will play an audio file while the user is using the app. The user will be able to continue to use the app after starting to play a file, and the file will be played/managed via a static class, rather than a single view (since that view may be removed from the navigation stack while the file is still being played).

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

I want to have a mini-player that is visible on any other view within the app, and I'm using a ControlTemplate to accomplish this. I want this control template to have some items that are bound to properties within the static class (such as player state [playing/paused], time remaining, title, etc.) as well as to have control to run methods upon the static class. I can run methods using code behind in my app.xaml page (where the ControlTemplate lives), but I'm having a hard time getting my bindable properties bound.

现在,我只有一个切换,应该将IsToggled绑定到可绑定的属性,将IsPlaying绑定到静态类的可绑定的属性.

Right now I just have a toggle where IsToggled is supposed to bound to a bindable property IsPlaying is bound to the static class' bindable property.

我继续收到以下错误:

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

I have defined the namespace in the XMLNS in all my .xaml so I'm not sure what's going on. My entire project is on GitHub at 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内部)

And here's my ControlTemplate (inside 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的工作方式不同.

Any thoughts? I'll admit I'm new to binding within Xamarin, but all that I'm reading seems to apply more to ContentPages and appears that it works differently with ControlTemplates.

推荐答案

我使用以下示例进行了此工作:

I got this working using the examples here: Binding to a property within a static class instance

问题不完全相同,所以我要离开我的问题并回答它.

It's not quite the same issue so I'm leaving my question and answering it.

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

I did change my class to match the answer above, but the big change is how the binding code looks in 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}" />

路径"似乎是最大的不同.猜猜我会在弹出时阅读更多内容.

The "Path" seems to be the big difference. Guess I'll be reading up on this some more as it just now popped up.

这篇关于Xamarin-绑定到ControlTemplate中的静态类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆