简体   繁体   English

在Windows 8 C#中更新UI

[英]Updating UI in windows 8 C#

I have a page called AllPageView which has a GridView. 我有一个名为AllPageView的页面,其中有一个GridView。 The datatemplate for the GridView is as follows GridView的数据模板如下

<GridView.ItemTemplate>
     <DataTemplate>
          <Canvas Width="{Binding TemplateWidth}" Height="{Binding TemplateHeight}">
              <Canvas.Background>
                  <ImageBrush ImageSource="{Binding PageBackground}"/>
               </Canvas.Background>
                <Image Height="{Binding TemplateHeight}" Width="{Binding TemplateWidth}" Source="{Binding Page}" Stretch="Uniform" Opacity="1" CacheMode="BitmapCache" />

                   <StackPanel x:Name="EditDeleteStackPanel" Width="{Binding TemplateWidth}" Height="{Binding TemplateHeight}" Opacity="0.95">
                       <Button x:Name="NoteDelete"  HorizontalAlignment="Right"   VerticalAlignment="Top" Foreground="{x:Null}" Tapped="NoteDelete_Tapped" MinWidth="50" MinHeight="50" Margin="0,0,10,0" BorderBrush="{x:Null}" >
                           <Button.Background>
                               <ImageBrush ImageSource="{Binding Delete}"/>
                           </Button.Background>
                       </Button>
                       <Button x:Name="NoteEdit"  HorizontalAlignment="Right"   VerticalAlignment="Top"   FontFamily="Segoe Script" FontSize="24" BorderBrush="{x:Null}" Tapped="NoteEdit_Tapped" Foreground="{x:Null}" MinWidth="50" MinHeight="50" Margin="0,0,10,0">
                           <Button.Background>
                               <ImageBrush ImageSource="{Binding Edit}"/>
                           </Button.Background>
                       </Button>
                  </StackPanel>
           </Canvas>
       </DataTemplate>
</GridView.ItemTemplate>

The Image has its height and width bound to TemplateHeight and TemplateWidth of the Page_Collection class respectively. Image的高度和宽度分别绑定到Page_Collection类的TemplateHeightTemplateWidth I have a better and setter for TemplateHeight and TemplateWidth . 我对TemplateHeightTemplateWidth有更好的选择。

public static int TemplateWidth
{
    get { return m_templateWidth; }
    set 
    { 
        m_templateWidth = value;                
    }
}

The problem is, I have now a need to resize the image sizes from a page called General . 问题是,我现在需要从称为General的页面调整图像大小。
When a toggle switch is toggled, I need to change the size of the image. 拨动开关时,我需要更改图像的大小。 Like this 像这样

private void OnCompactCategoryToggled(object sender, RoutedEventArgs e)
{
    if (compactCateg.IsOn == true)
    {
        Page_Collection.TemplateHeight = 100;
        Page_Collection.TemplateWidth = 350;
    }
    else
    {
        Page_Collection.TemplateHeight = 200;
        Page_Collection.TemplateWidth = 700;
    }
}

Though the AllPageView page is bound to Page_Collection , the values does not get updated and hence the image size is the same. 尽管AllPageView页面绑定到Page_Collection ,但是值不会更新,因此图像大小是相同的。 Genera l is a Flyout in the SettingsPane . Genera L是一个FlyoutSettingsPane

I'm very new to Windows 8 and this is my first time DataBinding . 我是Windows 8的新手,这是我第一次使用DataBinding Could someone please tell me where I'm going wrong or if I'm missing something? 有人可以告诉我我要去哪里错了,或者我想念什么吗?

EDIT 编辑

This is the code behind for AllPageView . 这是AllPageView的代码。 I call Load_PageCollection in the constructor of the class 我在类的构造函数中调用Load_PageCollection

public async void Load_PageCollection()
{
    m_pageConfig = new PageConfig();
    Page_Collection[] tmppage = await m_pageConfig.Read_FromJSONFile(App.PAGECONFIG);
    List<Page_Collection> tmp;
    if (tmppage != null)
    {
        for (int i = 0; i < tmppage.Length; i++)
        {
            tmppage[i].UpdateCompletionStatus();
        }
        tmp = tmppage.ToList();
        ObservableCollection<Page_Collection> NoteCol = new ObservableCollection<Page_Collection>(tmp.ToList<Page_Collection>());
        PageCollection = NoteCol;
        PageLV.DataContext = PageCollection;
        m_pageManager.InitilizeWithFileLoc(PageCollection.ToArray());
    }
    else
    {
        PageCollection = new ObservableCollection<Page_Collection>();
        PageLV.DataContext = PageCollection;// PageLV is the grid view. The gridview, the image and a stackpanel.
    }
}

Edited after updated code. 更新代码后进行编辑。

You will need to move your template properties into a separate class that supports INotifyPropertyChanged and then refer to this class from each of your PageCollection instances.... 您需要将模板属性移到支持INotifyPropertyChanged的单独的类中,然后从每个PageCollection实例中引用该类。

public class PageCollectionTemplate : INotifyPropertyChanged {

   private static readonly void m_Instance=new PageCollectionTemplate();

   private int m_templateWidth;

   public static PageCollectionTemplate Instance {
     get { return m_Instance; }
   }

   public  int TemplateWidth {
        get { return m_templateWidth; }

        set 
        { 
            if (m_templateWidth == value) return;
            m_templateWidth = value;                
            OnPropertyChanged("TemplateWidth");
        }
    }
    // Do same for template height...

   protected void OnPropertyChanged(string propertyName) {
     var handler=PropertyChanged;
     if (handler!=null) handler(this,new PropertyChangedEventArgs(propertyName));
  }

  public event PropertyChangedEventHandler PropertyChanged;

}

UPDATE, I missed out this bit before,iIn your page collection class 更新,我之前错过了这一点,i在您的页面收集类中

public PageCollectionTemplate Tempate { 
    get { return PageCollectionTemplate.Instance; }
}

Then update your bindings to be {Binding Template.TemplateWidth} . 然后将您的绑定更新为{Binding Template.TemplateWidth}

There are alternative implementations you could by setting the source of the binding to the singleton instance eg: 您可以通过设置绑定到单例实例的绑定源来实现其他方法,例如:

{Binding TemplateWidth,Source={x:Static w:PageCollectionTemplate.Instance}

But you will need to define the namespace for w 但是您将需要为w定义名称空间

Do not be tempted to implement the following because the notify property changed events will not fire! 不要试图实现以下内容,因为notify属性更改的事件将不会触发!

public int TemplateWidth {
   get { return PageCollectionTemplate.Instance.TemplateWidth; }
   set { PageCollectionTemplate.Instance.TemplateWidth=value; }
}

The class containing the definition for TemplateWidth needs to implement INotifyPropertyChanged, and the setter for TemplateWidth needs to raise the PropertyChanged event. 包含TemplateWidth定义的类需要实现INotifyPropertyChanged,而TemplateWidth的设置方法需要引发PropertyChanged事件。

public int TemplateWidth
{
    get { return m_templateWidth; }

    set 
    { 
        m_templateWidth = value;    
        if (PropertyChanged!=null)
        {
             PropertyChanged(this, new PropertyChangedEventArgs("TemplateWidth"));
        }            
    }
}

Using frameworks like Prism or SimpleMVVM can simplify the coding for this type of thing and also relieve you from relying on constants like "TemplateWidth". 使用Prism或SimpleMVVM之类的框架可以简化此类事物的编码,并且还可以避免依赖诸如“ TemplateWidth”之类的常量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM