2017-07-30 66 views
1

作爲一個新手,我正在嘗試爲autor開發一個應用程序。工作區頁面包含一個帶有兩個柱的網格。這些列(單個或兩個)的可視性由單選按鈕控制。每列包含WorkspaceUsercontrol的一個實例。
此用戶控件的splitview窗格將包含章節,場景等 - 內容將填充不同類型(文本,圖像,html等)的列表視圖項目應該可以將列表視圖項目從一個實例移動到其他北拖,所以一個實例的變化應該被另一個實例所反映。這是迄今爲止代碼:usercontrol的兩個實例

編輯 爲了使它更容易理解,我已經建立一個新的「剝離」項目。我使用了Windows模板工作室(空白,基本的MVVM)。

MainPage.xaml中

<Page 
    x:Class="Test.Views.MainPage" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:usercontrols="using:Test.Usercontrols" 
    xmlns:toolkit="using:Microsoft.Toolkit.Uwp.UI.Controls" 
    mc:Ignorable="d"> 
    <Grid 
     x:Name="ContentArea" 
     Margin="{StaticResource MediumLeftRightMargin}"> 

     <Grid.RowDefinitions> 
      <RowDefinition x:Name="TitleRow" Height="48"/> 
      <RowDefinition Height="*"/> 
     </Grid.RowDefinitions> 

     <TextBlock 
      x:Name="TitlePage" 
      x:Uid="Main_Title" 
      Style="{StaticResource PageTitleStyle}" /> 

     <Grid 
      Grid.Row="1" 
      Background="{ThemeResource SystemControlPageBackgroundChromeLowBrush}"> 
      <!--The SystemControlPageBackgroundChromeLowBrush background represents where you should place your content. 
       Place your content here.--> 
      <Grid 
       Grid.Row="0" 
       x:Name="WorkspaceGrid"> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition 
         x:Name="Workspace1Column" 
         Width="1*"></ColumnDefinition> 
        <ColumnDefinition 
         x:Name="WorkspaceSplitterColumn" 
         Width="auto"></ColumnDefinition> 
        <ColumnDefinition 
         x:Name="Workspace2Column" 
         Width="1*"></ColumnDefinition> 
       </Grid.ColumnDefinitions> 
       <!--workspace 1--> 
       <usercontrols:MainpageUsercontrol 
        x:Name="WorkspaceUC1" 
        Grid.Column="0"></usercontrols:MainpageUsercontrol> 
       <!--Column Grid Splitter--> 
       <toolkit:GridSplitter 
        x:Name="MainpageGridSplitter" 
        Grid.Column="1" 
        Width="11" 
        ResizeBehavior="BasedOnAlignment" 
        ResizeDirection="Auto" 
        Background="Gray" 
        Foreground="White" 
        FontSize="13"> 
        <toolkit:GridSplitter.Element> 
         <Grid> 
          <TextBlock 
           HorizontalAlignment="Center" 
           IsHitTestVisible="False" 
           VerticalAlignment="Center" 
           Text="&#xE784;" 
           Foreground="Black" 
           FontFamily="Segoe MDL2 Assets"></TextBlock> 
         </Grid> 
        </toolkit:GridSplitter.Element> 
       </toolkit:GridSplitter> 
       <!--workspace 2--> 
       <usercontrols:MainpageUsercontrol 
        x:Name="WorkspaceUC2" 
        Grid.Column="2"></usercontrols:MainpageUsercontrol> 
      </Grid> 
     </Grid> 
    </Grid> 
</Page> 

MainPage.xaml.cs中

using Test.ViewModels; 

using Windows.UI.Xaml.Controls; 

namespace Test.Views 
{ 
    public sealed partial class MainPage : Page 
    { 
     public MainViewModel ViewModel { get; } = new MainViewModel(); 
     public MainPage() 
     { 
      InitializeComponent(); 
     } 
    } 
} 

MainPageViewModel.cs

using System; 

using Test.Helpers; 

namespace Test.ViewModels 
{ 
    public class MainViewModel : Observable 
    { 
     public MainViewModel() 
     { 
     } 
    } 
} 

MainpageUsercontrol.xaml

<UserControl 
    x:Class="Test.Usercontrols.MainpageUsercontrol" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:local="using:Test.Usercontrols" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:converter="using:Test.Converters" 
    mc:Ignorable="d" 
    d:DesignHeight="300" 
    d:DesignWidth="400"> 
    <UserControl.Resources> 
     <converter:NullableBoolToBoolConverter 
      x:Key="NullableBoolToBoolConverter" /> 
     <Style 
      x:Key="HamburgerButtonStyle" 
      TargetType="ToggleButton"> 
      <Setter 
       Property="Content" 
       Value="&#xE700;" /> 
      <Setter 
       Property="FontFamily" 
       Value="Segoe MDL2 Assets" /> 
      <Setter 
       Property="FontSize" 
       Value="20" /> 
      <Setter 
       Property="MinHeight" 
       Value="48" /> 
      <Setter 
       Property="MinWidth" 
       Value="48" /> 
     </Style> 
    </UserControl.Resources> 
    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition 
       Height="auto"></RowDefinition> 
      <RowDefinition 
       Height="*"></RowDefinition> 
     </Grid.RowDefinitions> 
     <RelativePanel 
      Grid.Row="0" 
      Background="WhiteSmoke"> 
      <ToggleButton 
       x:Name="HamburgerButton" 
       Style="{StaticResource HamburgerButtonStyle}" 
       IsThreeState="False" 
       RelativePanel.AlignBottomWithPanel="True" 
       RelativePanel.AlignTopWithPanel="True" 
       RelativePanel.AlignLeftWithPanel="True" 
       IsChecked="{x:Bind WorkspaceSplitview.IsPaneOpen, Mode=TwoWay, Converter={StaticResource NullableBoolToBoolConverter}}"></ToggleButton> 
     </RelativePanel> 
     <SplitView 
      Grid.Row="1" 
      x:Name="WorkspaceSplitview" 
      IsPaneOpen="False"> 
      <SplitView.Pane> 
       <Grid Background="LightGray"> 
        <Grid.RowDefinitions> 
         <RowDefinition 
          Height="1*"></RowDefinition> 
        </Grid.RowDefinitions> 
        <TextBlock 
         Grid.Row="0" 
         Text="ToDo: Listview Navigation"></TextBlock> 
       </Grid> 
      </SplitView.Pane> 
      <SplitView.Content> 
       <TextBox 
        Background="BlanchedAlmond" 
        Height="60" 
        Width="200" 
        Text="ToDo: Content"></TextBox> 
      </SplitView.Content> 
     </SplitView> 
    </Grid> 
</UserControl> 

MainpageUsercontrol.xaml.cs

using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Runtime.InteropServices.WindowsRuntime; 
using Windows.Foundation; 
using Windows.Foundation.Collections; 
using Windows.UI.Xaml; 
using Windows.UI.Xaml.Controls; 
using Windows.UI.Xaml.Controls.Primitives; 
using Windows.UI.Xaml.Data; 
using Windows.UI.Xaml.Input; 
using Windows.UI.Xaml.Media; 
using Windows.UI.Xaml.Navigation; 

// The User Control item template is documented at https://go.microsoft.com/fwlink/?LinkId=234236 

namespace Test.Usercontrols 
{ 
    public sealed partial class MainpageUsercontrol : UserControl 
    { 
     public MainpageUsercontrol() 
     { 
      this.InitializeComponent(); 
     } 
    } 
} 

NullableBoolToBoolConverter

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using Windows.UI.Xaml.Data; 

namespace Test.Converters 
{ 
    public class NullableBoolToBoolConverter : IValueConverter 
    { 
     public object Convert(object value, Type targetType, object parameter, string language) 
     { 
      bool? val = (bool?)value; 
      return val.HasValue ? val.Value : false; 
     } 

     public object ConvertBack(object value, Type targetType, object parameter, string language) => value; 
    } 
} 

的問題:當我點擊用戶控件的按鈕實例1,打開此實例的SPLITVIEW窗格中,但是當我點擊然後在實例2的按鈕上關閉實例1的分割視圖窗格。這是爲什麼?

我用Google搜索了很多關於在UWP用戶控件,但我沒有發現任何深入瞭解他們的工作:

  • 什麼封裝(事件,屬性)?
  • usercontrols如何與調用頁面進行通信?
  • 他們如何相互溝通?
  • usercontrol.content如何工作?

希望我不是唯一對usercontrols有點困惑的人。

更新 下圖顯示單擊該按鈕之前,活立木: Before

,這表明當拆分窗格已經雙頭呆活樹: After

也許這個「彈出根「是解決方案的一個暗示?

+1

我測試了你的上面的代碼片段,但不能重現你上面提到的問題。你能分享一個[mcve]嗎? –

+0

我已編輯代碼以使其更全面。我希望你現在可以重現這個問題。 謝謝你的幫助。 – user3350279

回答

0

無意中我找到了解決辦法:我添加

DISPLAYMODE = 「內聯」

我的SPLITVIEW。我不知道這是爲什麼,但我不會抱怨。我只會感激地接受;-)

無論如何:有人能夠解釋這種行爲嗎?