English 中文(简体)
森林小组的方法是什么,我可以用来把一些财产放在控制上第一次展示?
原标题:what WPF method can I use to set some properties on a control s first display?
  • 时间:2011-06-15 09:14:07
  •  标签:
  • c#
  • wpf
  • xaml

我需要储存我的窗户大小/位置,并在用户关闭时说明,然后在用户开放电灯时将其带回。

我是很容易利用登记处钥匙(甚至这是这样做的最佳方式)这样做的,但我很想知道,我究竟应该把守则放在什么地方来确定这些特性。

我认为,在窗户第一“耳朵”时,我需要确定。 但我有几种方法可以用于本案:

  • Window.Show()
  • Window.Activate()
  • Window.ApplyTemplate()
  • Window.Arrange()
  • Window.ArrangeCore()
  • Window.ArrangeOverride()
  • Window.BeginInit()
  • Window.EndInit()
  • Window.Measure()
  • Window.MeasureCore()
  • Window.MeasureOverride()
  • Window.OnApplyTemplate()
  • Window.OnInitialized()
  • Window.OnRender()
  • Window.UpdateLayout()

我知道,他们当中大多数只是一个坏的想法(例如,现在的Layout(Awaaaaaaaaay)经常被称作瓦阿。 理想的情况是,我只想寻找一种在窗户一生中只需要一次的方法,这样,如果这是首选的方法,我就不必增加旗帜检查。

so which one would be the best in this case? and why?

次要问题: 我把该守则放在温得和中。 接近(在我<代码>MyWindow类别中高于方法),但我也可以把它放在温得。 OnClosing() or Window.OnClosed(). 这是否对我的案件有任何影响?

次要问题(之二): 我还必须节省一个数据格列单,在这种情况下,我是否应当把“节省”和“装载”代码放在哪里?

最佳回答

我的选择: 最后,我最后把该守则放在窗户上从登记处输入价值。 显示。

我之所以这样做,是因为有两点:

  1. 我储存窗户的状况(管理/最大化)以及万国邮联的运作方式,我需要首先确定出头巾,然后确定最大状态(如果需要的话),否则就会评估布局。 而且,如果我不先打上头巾,那么当我从那时起把窗户最大化时,我就会放松。 因此,我必须按这一顺序行事: Width + 高地及其状态。 (此外,在使用多个屏幕时,有必要这样做,否则你会放松你正在使用的屏幕。) 这意味着上述某些方法不切实际(例如“措施”方法)。

  2. 此外,如果我把我的代码放在上述多数方法中的话,我就会首先看到窗户的高度和宽度,在屏幕中,然后在稍有拖延之后,窗户将尽量扩大。

putting the code in window.Show() managed to solve those 2 issues. I might have had the same result with one or more of the other methods, but I simply got fed-up with trying different configurations and ended up using the first one that gave me entire satisfaction.

问题回答

Ok, it seems to me you are treating WPF like you would an old-school WinForms application. You no longer need to monitor form events to retrieve information from the Forms Properties. A majority of WPF Control properties are something known as a Dependency Property.

Amonst some of the clever things dependency properties introduce is Data Binding.

If you then consider writing the application with an MVVM Architecture you will quickly be able to work the following out yourself... =)

In the View*1, you can create either Dependency Properties, or standard properties and implement INotifyPropertyChanged, which hold the Size/Layout/Position/etc. Then bind the form s properties (in xaml or code) to the View s Properties. You can then implement any functionality you like for storing/retrieving the defaults and have automatic updates when the form is changed by simply adapting the Get/Set of the properties in the view.

As a quick example on the Windows Title:

<Window x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="{Binding Path=DisplayName}"
        WindowStartupLocation="CenterScreen" >
    <Grid>...</Grid>
</Window>

An example implementation of the view:

public class SampleView : System.ComponentModel.INotifyPropertyChanged
{

    public event PropertyChangedEventHandler System.ComponentModel.INotifyPropertyChanged.PropertyChanged;
    public delegate void PropertyChangedEventHandler(object sender, System.ComponentModel.PropertyChangedEventArgs e);

    private string _Title;
    public string Title {
        get {
            if (_Title == null) {
                _Title = My.Settings.MainWindowTitle;
            }
            return _Title;
        }
        set {
            _Title = value;
            if (!(_Title == My.Settings.MainWindowTitle)) {
                if (PropertyChanged != null) {
                    PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs("Title"));
                }
                My.Settings.MainWindowTitle = Title;
                My.Settings.Save();
            }
        }
    }
}

EDIT:关于如何最好地储存用户的偏好,我建议登记处,但远未受到重视。 登记册现在全程包装,在我的意见<>中,登记册实际上没有设计。 考虑使用。 这将处理储存/检索数据地点和方式的大部分分册,并为您提供冰类安全接口。

*1 I personally prefer to try and bind everything to the ViewModel and have almost totally dumb Views; though I do know there are plenty of valid cases for the Views having code. I wouldn t say the Size/Layout/etc is really a Business Logic Concern and isn t something I ve concerned myself with up to now, so this should probably be handled in the View itself.

EDIT 2 - A quick example of User/Application scope settings:

Here is a quick picture of the settings I added to the project:

Settings screenshot

The following code attempts to use both the application and user scoped settings. NB: Application Scope Settings are ReadOnly at runtime

public class SettingsExample
{
    private Form1 frmMain = new Form1();

    public void Main()
    {
        frmMain.BackColor = My.Settings.DefaultBackColour;
    }

    public void UserLoggedIn()
    {
        frmMain.BackColor = My.Settings.UserBackcolour;
    }

    public void UpdateUserBackcolour(System.Drawing.Color newColour)
    {
        My.Settings.UserBackcolour = newColour;
        My.Settings.Save();
    }

    public void UpdateDefaultBackcolour(System.Drawing.Color newColour)
    {
        My.Settings.DefaultBackColour = newColour;
        // Compiler Error
        // This property is read only because it is an application setting
        // Only user settings can be changed at runtime
    }

}

First off, you forgot about

Loaded event - occurs when the element is laid out, rendered, and ready for interaction. (Inherited from FrameworkElement.)

没有一个简单的答案。 假设情况可能有所不同,它是否是一个儿童“类似方言”窗口(当时,我只是将体积确定在显示之前的线段)、同一窗口的新例子或新笔照。

I will argue that UpdateLayout() is a bad idea. Actually, it s a very good idea. For example:

private bool m_onStart = true;

public MainWindow()
{
        InitializeComponent();
        this.LayoutUpdated += new EventHandler(MainWindow_LayoutUpdated);
}

void MainWindow_LayoutUpdated(object sender, EventArgs e)
{
     if (m_onStart)
     {
         m_onStart = false;
         Dispatcher.BeginInvoke(() =>
         {
              //Start App
         }
         );
     }
 }

Even if it s called a thousand time per second (which is very unlikely) you won t even notice it and won t hurt the performance.

总的来说,你可以采用一种避免用户偏好的助手方法,其次为用户。 由于这项任务涉及并使用微型和小型设备,因此具有约束力,因此在Loaded活动(当所有机器人、原始树木和视觉树都做时)中设定了规模。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...