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

查看:24
本文介绍了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天全站免登陆