C++/WinRT XAML 控件绑定到属性

问题描述

我试图通过引入另一个 ViewModel 来扩展书店示例,以便在书店内拥有部分。我的情况我称之为“图书馆”。我很难将添加的书籍显示在第二个 ListView 中。在 MainPage 中,除了 MainViewModel(现在显示部分)之外,我还添加了 SubModelView 以返回当前部分。在我的 Xaml 代码中,我用代码构造了第二个 ListView 来显示书籍,但它不起作用。任何人都可以建议我做错了什么。我在 MainPage 中包含了一个 SubModelView 以访问正在创建书籍的当前部分,并在 Xaml 代码中使用它。

#include "pch.h"
#include "MainPage.h"
#include "MainPage.g.cpp"


using namespace winrt;
using namespace Windows::UI::Xaml;
using namespace Windows::Foundation::Collections;
using namespace Windows::UI::Popups;

namespace winrt::Library::implementation
{
    MainPage::MainPage()
    {
        m_mainViewModel = winrt::make<Library::implementation::LibraryViewModel>();
        InitializeComponent();
    }

    Library::LibraryViewModel MainPage::MainViewModel()
    {
        return m_mainViewModel;
    }

    Library::Section MainPage::SubViewModel()
    {
        m_subViewModel = MainViewModel().Sectioncurrent();
        return m_subViewModel;
    }
}

void winrt::Library::implementation::MainPage::SectionButton_Click(winrt::Windows::Foundation::IInspectable const& sender,winrt::Windows::UI::Xaml::RoutedEventArgs const& e)
{
    hstring sname{ Section_name().Text().c_str() };
    MainViewModel().Sectioncurrent(sname);
}


void winrt::Library::implementation::MainPage::TotleButton_Click(winrt::Windows::Foundation::IInspectable const& sender,winrt::Windows::UI::Xaml::RoutedEventArgs const& e)
{
    if (MainViewModel().Sectioncurrent() == nullptr)
        return;
    hstring bname{ Title_name().Text().c_str() };
    MainViewModel().Sectioncurrent().Bookcurrent(bname);

    Library::Section subsec = SubViewModel();
    Library::Book bk = subsec.Bookcurrent();
    hstring tt = bk.Title();
}
#include "pch.h"
#include "LibraryViewModel.h"
#include "LibraryViewModel.g.cpp"

// Note: Remove this static_assert after copying these generated source files to your project.
// This assertion exists to avoid compiling these generated source files directly.
// static_assert(false,"Do not compile generated C++/WinRT source files directly");

namespace winrt::Library::implementation
{
    LibraryViewModel::LibraryViewModel()
    {
        m_section = winrt::single_threaded_observable_vector<Library::Section>();
    }

    Windows::Foundation::Collections::IObservableVector<Library::Section> LibraryViewModel::Sections()
    {
        return m_section;
    }

    void LibraryViewModel::Sectioncurrent(hstring const& sectionname)
    {
        m_sectionCurrent = winrt::make<Library::implementation::Section>(sectionname);
        Sections().Append(m_sectionCurrent);
    }

    Library::Section LibraryViewModel::Sectioncurrent()
    {
        return m_sectionCurrent;
    }

    winrt::event_token LibraryViewModel::PropertyChanged(Windows::UI::Xaml::Data::PropertyChangedEventHandler const& handler)
    {
        return m_propertyChanged.add(handler);
    }

    void LibraryViewModel::PropertyChanged(winrt::event_token const& token) noexcept
    {
        m_propertyChanged.remove(token);
    }
}
#include "pch.h"
#include "Section.h"
#include "Section.g.cpp"

// Note: Remove this static_assert after copying these generated source files to your project.
// This assertion exists to avoid compiling these generated source files directly.
// static_assert(false,"Do not compile generated C++/WinRT source files directly");

namespace winrt::Library::implementation
{
   // Constructor
   Section::Section(hstring const& name) : m_sectionName{name}
   {
       m_section = winrt::single_threaded_observable_vector<Library::Book>();
   }

   Windows::Foundation::Collections::IObservableVector<Library::Book> Section::Books()
   {
       return m_section;
   }

   void Section::Bookcurrent(hstring const& name)
   {
       m_book = winrt::make<Library::implementation::Book>(name);
       Books().Append(m_book);
   }

   Library::Book Section::Bookcurrent()
   {
       return m_book;
   }

    hstring Section::SectionName()
    {
        return m_sectionName;
    }

    winrt::event_token Section::PropertyChanged(Windows::UI::Xaml::Data::PropertyChangedEventHandler const& handler)
    {
        return m_propertyChanged.add(handler);
    }

    void Section::PropertyChanged(winrt::event_token const& token) noexcept
    {
        m_propertyChanged.remove(token);
    }
}
<Page
    x:Class="Library.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Library"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        <TextBox x:Name="Section_name" Grid.Row="0" Grid.Column="0" Margin="160,5,5" 
                 HorizontalAlignment="Left"
                 MinWidth="200"/>
        <TextBox x:Name="Title_name" Grid.Row="0" Grid.Column="1" Margin="5,160,5" 
                 HorizontalAlignment="Right"
                 MinWidth="200"/>
        <Button x:Name="SectionButton" Grid.Row="1" Grid.Column="0" 
                Click="SectionButton_Click" Margin="160,5"
                HorizontalAlignment="Left">Add Section</Button>
        <Button x:Name="TotleButton" Grid.Row="1" Grid.Column="1" 
                Click="TotleButton_Click" Margin="5,5"
                HorizontalAlignment="Right">Add Title</Button>
        <ListView x:Name="SectionList"
                  HorizontalAlignment="Stretch"
                  VerticalAlignment="Stretch"
                  Grid.Row="2" Grid.Column="0"
                  BorderBrush="Black" BorderThickness="2"
                  Margin="40,10,40,10"
                  ItemsSource="{x:Bind MainViewModel.Sections}">
            <ListView.ItemTemplate>
                <DataTemplate x:DataType="local:Section">
                    <TextBlock Text="{x:Bind SectionName,Mode=OneWay}"/>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
        <ListView x:Name="TitleList"
                  HorizontalAlignment="Stretch"
                  VerticalAlignment="Stretch"
                  Grid.Row="2" Grid.Column="1"
                  BorderBrush="Black" BorderThickness="2"
                  Margin="40,10"
                  ItemsSource="{x:Bind SubViewModel.Books}">
            <ListView.ItemTemplate>
                <DataTemplate x:DataType="local:Book">
                    <TextBlock Text="{x:Bind Title,Mode=OneWay}"/>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
        <Button x:Name="Process" Grid.Row="3" Grid.Column="0"
                Grid.ColumnSpan="2"
                HorizontalAlignment="Center"
                Click="Process_Click">PROCESS
        </Button>
    </Grid>
</Page>
import "LibraryViewModel.idl";

namespace Library
{
    [default_interface]
    runtimeclass MainPage : Windows.UI.Xaml.Controls.Page
    {
        MainPage();
        LibraryViewModel MainViewModel{ get; };
    }
}
import "SectionViewModel.idl";

namespace Library
{
    runtimeclass LibraryViewModel : Windows.UI.Xaml.Data.INotifyPropertyChanged
    {
        LibraryViewModel();
        Windows.Foundation.Collections.IObservableVector<SectionViewModel> Sections{ get; };
        void SectionViewModel(String sname);
        SectionViewModel SubViewModel{ get; };
    }
}
import "Book.idl";

namespace Library
{
    [bindable]
    runtimeclass SectionViewModel : Windows.UI.Xaml.Data.INotifyPropertyChanged
    {
        SectionViewModel(String name);
        String SectionName{ get; };
        Windows.Foundation.Collections.IObservableVector<Book> Books{ get; };
        void Book(String sname);
        Book BookCurrent{ get; };
    }
}
namespace Library
{
    [bindable]
    runtimeclass Book : Windows.UI.Xaml.Data.INotifyPropertyChanged
    {
        Book(String title);
        String Title{ get; };
    }
}
#pragma once

#include "MainPage.g.h"
#include "LibraryViewModel.h"

namespace winrt::Library::implementation
{
    struct MainPage : MainPageT<MainPage>
    {
        MainPage();
        Library::LibraryViewModel MainViewModel();
        
        void SectionButton_Click(winrt::Windows::Foundation::IInspectable const& sender,winrt::Windows::UI::Xaml::RoutedEventArgs const& e);
        void TitleButton_Click(winrt::Windows::Foundation::IInspectable const& sender,winrt::Windows::UI::Xaml::RoutedEventArgs const& e);
        void Process_Click(winrt::Windows::Foundation::IInspectable const& sender,winrt::Windows::UI::Xaml::RoutedEventArgs const& e);
    private:
        Library::LibraryViewModel m_mainViewModel{ nullptr };
        Library::SectionViewModel m_sectionCurrent{ nullptr };
        winrt::event<Windows::UI::Xaml::Data::PropertyChangedEventHandler> m_propertyChanged;
       
    };
}

namespace winrt::Library::factory_implementation
{
    struct MainPage : MainPageT<MainPage,implementation::MainPage>
    {
    };
}
#pragma once
#include "LibraryViewModel.g.h"
#include "SectionViewModel.h"

// Note: Remove this static_assert after copying these generated source files to your project.
// This assertion exists to avoid compiling these generated source files directly.
// static_assert(false,"Do not compile generated C++/WinRT source files directly");

namespace winrt::Library::implementation
{
    struct LibraryViewModel : LibraryViewModelT<LibraryViewModel>
    {
        LibraryViewModel();
        
        Windows::Foundation::Collections::IObservableVector<Library::SectionViewModel> Sections();
        void SectionViewModel(hstring const& sname);
        Library::SectionViewModel SubViewModel();

        winrt::event_token PropertyChanged(Windows::UI::Xaml::Data::PropertyChangedEventHandler const& handler);
        void PropertyChanged(winrt::event_token const& token) noexcept;
    private:
        Windows::Foundation::Collections::IObservableVector<Library::SectionViewModel> m_section{ nullptr };
        Library::SectionViewModel m_sectionCurrent{ nullptr };
        winrt::event<Windows::UI::Xaml::Data::PropertyChangedEventHandler> m_propertyChanged;
    };
}
namespace winrt::Library::factory_implementation
{
    struct LibraryViewModel : LibraryViewModelT<LibraryViewModel,implementation::LibraryViewModel>
    {
    };
}
#pragma once
#include "SectionViewModel.g.h"
#include "Book.h"

// Note: Remove this static_assert after copying these generated source files to your project.
// This assertion exists to avoid compiling these generated source files directly.
// static_assert(false,"Do not compile generated C++/WinRT source files directly");

namespace winrt::Library::implementation
{
    struct SectionViewModel : SectionViewModelT<SectionViewModel>
    {
        SectionViewModel() = delete;
        SectionViewModel(hstring const& name);
        Windows::Foundation::Collections::IObservableVector<Library::Book> Books();
        hstring SectionName();
        void Book(hstring const& bname);
        Library::Book BookCurrent();
       
        winrt::event_token PropertyChanged(Windows::UI::Xaml::Data::PropertyChangedEventHandler const& handler);
        void PropertyChanged(winrt::event_token const& token) noexcept;
    private:
        Windows::Foundation::Collections::IObservableVector<Library::Book> m_books{ nullptr };
        hstring m_sectionName;
        Library::Book m_bookCurrent{ nullptr };
        winrt::event<Windows::UI::Xaml::Data::PropertyChangedEventHandler> m_propertyChanged;
    };
}
namespace winrt::Library::factory_implementation
{
    struct SectionViewModel : SectionViewModelT<SectionViewModel,implementation::SectionViewModel>
    {
    };
}
#pragma once
#include "Book.g.h"

// Note: Remove this static_assert after copying these generated source files to your project.
// This assertion exists to avoid compiling these generated source files directly.
// static_assert(false,"Do not compile generated C++/WinRT source files directly");

namespace winrt::Library::implementation
{
    struct Book : BookT<Book>
    {
        Book() = delete;
        Book(hstring const& title);
       // void Title(hstring const& value);
        hstring Title();
        winrt::event_token PropertyChanged(Windows::UI::Xaml::Data::PropertyChangedEventHandler const& handler);
        void PropertyChanged(winrt::event_token const& token) noexcept;
    private:
        hstring m_title;
        winrt::event<Windows::UI::Xaml::Data::PropertyChangedEventHandler> m_propertyChanged;
    };
}
namespace winrt::Library::factory_implementation
{
    struct Book : BookT<Book,implementation::Book>
    {
    };
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)