如何在UWP XAML中双向绑定用户控件? (C ++ / WinRT)

时间:2019-02-28 06:36:01

标签: xaml uwp c++-winrt

我正在尝试以两种方式在@Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.main, menu); //setup SearchView and callbacks final MenuItem searchItem = menu.findItem(R.id.action_search); mSearchView = (SearchView) searchItem.getActionView(); if(mSavedInstanceState != null){ switch (mSavedInstanceState.getInt("SearchViewVisibility")){ case View.VISIBLE: mSearchView.setVisibility(View.VISIBLE); case View.INVISIBLE: mSearchView.setVisibility(View.INVISIBLE); case View.GONE: mSearchView.setVisibility(View.GONE); } } mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener(){ @Override public boolean onQueryTextSubmit(String query) { SetFragmentAndFilter(query); return true; } @Override public boolean onQueryTextChange(String query) { // check whether this change is clearing all text from the search if(query.isEmpty()){ // close the searchView mSearchView.post(new Runnable(){ @Override public void run(){ mSearchView.clearFocus(); } }); mSearchView.setIconified(true); } //have the list match the new query text SetFragmentAndFilter(query); return true; } }); return true; } 内绑定数据。下面的代码适用于单向绑定,但是当我尝试使用双向绑定时,我从xaml编译器中收到以下错误:

UserControl

但是,这让我非常困惑,因为我确实将XamlCompiler error WMC1118: TwoWay binding target 'value_prop' must be a dependency property 设置为依赖项属性。我该如何工作?

// MainPage.xaml

value_prop

// attribute_slider.xaml

  <local:attribute_slider Label="Scale" value_prop="{x:Bind texture_showcase_vm.current_texture.scale, Mode=TwoWay}"/>

// attribute_slider.idl

<UserControl
    x:Class="wzrd_editor.attribute_slider"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:wzrd_editor"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" >
    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center" >
        <TextBlock x:Name="scale_label" Text="{x:Bind Label, Mode=OneWay}" HorizontalAlignment="Left" />
        <TextBlock x:Name="scale_text_value" Text="{x:Bind value_prop, Mode=TwoWay}" HorizontalAlignment="Right"
                                       RelativePanel.AlignRightWithPanel="True"
                                       RelativePanel.RightOf="scale_label" />

        <Slider x:Name="Scale" Value="{x:Bind value_prop, Mode=TwoWay}" Width="200" Orientation="Horizontal" Minimum="0" Maximum="10" StepFrequency="0.01" TickFrequency="0.01" SnapsTo="StepValues" TickPlacement="None"
                                   RelativePanel.Below="scale_label" 
                                   RelativePanel.AlignRightWith="scale_text_value"
                                   RelativePanel.AlignLeftWith="scale_label"
                                   RelativePanel.AlignRightWithPanel="True" />

    </StackPanel>
</UserControl>

attribute_slider.h

namespace wzrd_editor
{
    [bindable]
    [default_interface]
    runtimeclass attribute_slider : Windows.UI.Xaml.Controls.UserControl, Windows.UI.Xaml.Data.INotifyPropertyChanged
    {
        attribute_slider();
        static Windows.UI.Xaml.DependencyProperty LabelProperty{ get; };
        static Windows.UI.Xaml.DependencyProperty value_property{ get; set; };
        String Label;
        Single value_prop;
    }
}

// attribute_slider.cpp

namespace winrt::wzrd_editor::implementation
{
    struct attribute_slider : attribute_sliderT<attribute_slider>
    {
        attribute_slider();

        static Windows::UI::Xaml::DependencyProperty LabelProperty();
        static Windows::UI::Xaml::DependencyProperty value_property();
        static void value_property(Windows::UI::Xaml::DependencyProperty const& value);

        hstring Label();
        void Label(hstring const& value);

        float value_prop();
        void value_prop(float const& value);

        winrt::event_token PropertyChanged(Windows::UI::Xaml::Data::PropertyChangedEventHandler const& handler);
        void PropertyChanged(winrt::event_token const& token) noexcept;

    private:
        static Windows::UI::Xaml::DependencyProperty m_labelProperty;
        static Windows::UI::Xaml::DependencyProperty m_value_property;
        hstring m_label = L"";
        float m_value = 0.0f;

        winrt::event<Windows::UI::Xaml::Data::PropertyChangedEventHandler> m_property_changed;

        void raise_property_changed(hstring const& property_name)
        {
            m_property_changed(*this, Windows::UI::Xaml::Data::PropertyChangedEventArgs(property_name));
        }

        template <class T>
        void update_value(hstring const& property_name, T & var, T value)
        {
            if (var != value)
            {
                var = value;
                raise_property_changed(property_name);
            }
        }
    };
}

0 个答案:

没有答案