OpenFileDialog 在 WPF 中保存背景

发布于 2024-11-26 15:16:57 字数 2616 浏览 1 评论 0原文

我有一个调用 OpenFileDialog.ShowDialog 的 WPF 应用程序。当此对话框打开时,我的应用程序可能会改变背景并显示新信息,这是预期的行为。

如果用户现在关闭此对话框,背景将恢复,这意味着屏幕上有旧信息。

如何防止 OpenFileDialog 保存其背景?

或者,如果这是不可能的,我如何强制重新绘制我的应用程序?

示例代码,按下按钮并将对话框置于文本上:

<Window x:Class="BackgroundOfFileOpen.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="10*" />
        <RowDefinition Height="1*" />
    </Grid.RowDefinitions>
    <Viewbox Grid.Row="0">
        <Label Content="{Binding textInBackground}" />
    </Viewbox>
    <Button Grid.Row="1" Click="OnOpenDialog">Open Dialog</Button>
</Grid>

using Microsoft.Win32;
using System.Windows;
using System.Threading;
using System;

namespace BackgroundOfFileOpen
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public string textInBackground
        {
            get { return (string)GetValue(textInBackgroundProperty); }
            set { SetValue(textInBackgroundProperty, value); }
        }

        // Using a DependencyProperty as the backing store for textInBackground.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty textInBackgroundProperty = 
            DependencyProperty.Register("textInBackground", typeof(string), typeof(MainWindow), new UIPropertyMetadata("Text"));


        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;
            function += ModifyText;
        }

        private void OnOpenDialog(object sender, RoutedEventArgs e)
        {
            Thread backgroundThread = new Thread(ThreadMethod);
            backgroundThread.Start();

            OpenFileDialog dlg = new OpenFileDialog();
            dlg.ShowDialog();
        }

        public void ModifyText()
        {
            if (Dispatcher.CheckAccess())
            {
                textInBackground += "x";
            }
            else
            {
                Dispatcher.BeginInvoke(new Action(() => { ModifyText(); }));
            }
        }

        delegate void ModifyFunction();
        static ModifyFunction function;

        static void ThreadMethod()
        {
            Thread.Sleep(1000);
            function();
        }

    }
}

I have a WPF application which calls OpenFileDialog.ShowDialog. While this dialog is open it is possible and expected behavior that my applicaton alters the background and shows new information.

If the user now closes this dialog the background is restored, which means there is old information on the screen.

How can I prevent the OpenFileDialog from saving it's background?

Or if this is not possible, how can I force a repaint of my application?

Sample code, press button and position dialog over text:

<Window x:Class="BackgroundOfFileOpen.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="10*" />
        <RowDefinition Height="1*" />
    </Grid.RowDefinitions>
    <Viewbox Grid.Row="0">
        <Label Content="{Binding textInBackground}" />
    </Viewbox>
    <Button Grid.Row="1" Click="OnOpenDialog">Open Dialog</Button>
</Grid>

using Microsoft.Win32;
using System.Windows;
using System.Threading;
using System;

namespace BackgroundOfFileOpen
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public string textInBackground
        {
            get { return (string)GetValue(textInBackgroundProperty); }
            set { SetValue(textInBackgroundProperty, value); }
        }

        // Using a DependencyProperty as the backing store for textInBackground.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty textInBackgroundProperty = 
            DependencyProperty.Register("textInBackground", typeof(string), typeof(MainWindow), new UIPropertyMetadata("Text"));


        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;
            function += ModifyText;
        }

        private void OnOpenDialog(object sender, RoutedEventArgs e)
        {
            Thread backgroundThread = new Thread(ThreadMethod);
            backgroundThread.Start();

            OpenFileDialog dlg = new OpenFileDialog();
            dlg.ShowDialog();
        }

        public void ModifyText()
        {
            if (Dispatcher.CheckAccess())
            {
                textInBackground += "x";
            }
            else
            {
                Dispatcher.BeginInvoke(new Action(() => { ModifyText(); }));
            }
        }

        delegate void ModifyFunction();
        static ModifyFunction function;

        static void ThreadMethod()
        {
            Thread.Sleep(1000);
            function();
        }

    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ま柒月 2024-12-03 15:16:57

如何强制重新绘制我的应用程序?

关闭对话框后使用 UIExtensions.Refresh(this);

public static class UIExtensions
{
    public static void Refresh(this UIElement uiElement)
    {
        uiElement.Dispatcher.Invoke(DispatcherPriority.Render, new Action(() => { }));
    }
}

how can I force a repaint of my application?

After closing the dialog use UIExtensions.Refresh(this);

public static class UIExtensions
{
    public static void Refresh(this UIElement uiElement)
    {
        uiElement.Dispatcher.Invoke(DispatcherPriority.Render, new Action(() => { }));
    }
}
何时共饮酒 2024-12-03 15:16:57

对于任何感兴趣的人,我现在找到的唯一解决方法是在 ShowDialog 之后调用以下函数。如果窗口最大化,这不太好,并且会闪烁,但它适用于所有经过测试的系统。

        void RefreshWindow()
    {
        switch (WindowState)
        {
            case WindowState.Maximized:
                {
                    double oldWidth = Width;
                    Width = System.Windows.SystemParameters.PrimaryScreenWidth - 1;
                    WindowState = System.Windows.WindowState.Normal;
                    WindowState = System.Windows.WindowState.Maximized;
                    Width = oldWidth;
                }
                break;
            case WindowState.Normal:
                if (Width > 1)
                {
                    Width -= 1;
                    Width += 1;
                }
                else
                {
                    Width += 1;
                    Width -= 1;
                }
                break;
            case WindowState.Minimized:
            default:
                // no action necessary
                break;
        }
    }

For anyone who is interested, the only workaround I found by now is to call following function after ShowDialog. It's not nice, and it flickers, if the window is maximized, but it works on all tested systems.

        void RefreshWindow()
    {
        switch (WindowState)
        {
            case WindowState.Maximized:
                {
                    double oldWidth = Width;
                    Width = System.Windows.SystemParameters.PrimaryScreenWidth - 1;
                    WindowState = System.Windows.WindowState.Normal;
                    WindowState = System.Windows.WindowState.Maximized;
                    Width = oldWidth;
                }
                break;
            case WindowState.Normal:
                if (Width > 1)
                {
                    Width -= 1;
                    Width += 1;
                }
                else
                {
                    Width += 1;
                    Width -= 1;
                }
                break;
            case WindowState.Minimized:
            default:
                // no action necessary
                break;
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文