كيفية تحميل عنصر تحكم مستخدم جديد دون فتح نافذة جديدة في WPF؟

StackOverflow https://stackoverflow.com/questions/819216

  •  03-07-2019
  •  | 
  •  

سؤال

وضعت تطبيق WPF هذا http://tanguay.info/web/index.php?pg=codeExamples&id=164 معاً

http://tanguay.info/web/index.php?pg=codeExamples&id=164

الذي يقرأ ملف XML الخاص بالعملاء، ويسمح للمستخدم بتحريره وحفظه مرة أخرى، وكل شيء يعمل بشكل جيد.

ومع ذلك، عندما ينقر المستخدم على "حفظ" في صفحة "إدارة العملاء"، أريد أن "يعود" التطبيق إلى صفحة "إظهار العملاء".

"الصفحات" هي عناصر تحكم المستخدم التي يتم تحميلها ديناميكيًا في الصدفة مثل هذا:

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Reflection;
using TestDynamicForm123.View;

namespace TestDynamicForm123
{
    public partial class Shell : Window
    {
        private Dictionary<string, IBaseView> _userControls = new Dictionary<string, IBaseView>();

        public Dictionary<string, IBaseView> GetUserControls()
        {
            return _userControls;
        }

        public Shell()
        {
            InitializeComponent();

            List<string> userControlKeys = new List<string>();
            userControlKeys.Add("WelcomeView");
            userControlKeys.Add("CustomersView");
            userControlKeys.Add("ManageCustomersView");
            Type type = this.GetType();
            Assembly assembly = type.Assembly;
            foreach (string userControlKey in userControlKeys)
            {
                string userControlFullName = String.Format("{0}.View.{1}", type.Namespace, userControlKey);
                IBaseView userControl = (IBaseView)assembly.CreateInstance(userControlFullName);
                _userControls.Add(userControlKey, userControl);
            }

            //set the default page
            btnWelcome.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
        }

        private void btnGeneral_Click(object sender, RoutedEventArgs e)
        {
            PanelMainContent.Children.Clear();
            Button button = (Button)e.OriginalSource;
            PanelMainWrapper.Header = button.Content;
            Type type = this.GetType();
            Assembly assembly = type.Assembly;

            IBaseView userControl = _userControls[button.Tag.ToString()] as IBaseView;
            userControl.SetDataContext();


            PanelMainContent.Children.Add(userControl as UserControl);
        }
    }
}

لذلك عندما إدارة عرض العملاء يتم تحميله ويتعامل مع النقرة، ثم أحاول العودة إلى ملف عرض العملاء الصفحة التي تعمل، ولكنها تفتح نافذة جديدة لذلك في كل مرة يقوم المستخدم بتحرير عميل، تظهر نافذة جديدة بعد ذلك.

private void OnSave(object sender, System.Windows.Input.ExecutedRoutedEventArgs e)
{
    Customer customer = e.Parameter as Customer;
    Customer.Save(customer);

    //go back to default back
    Shell shell = new Shell();
    Button btnCustomers = shell.FindName("btnCustomers") as Button;
    btnCustomers.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
    shell.Show();
}

كيف يمكنني تغيير الكود أعلاه داخل UserControl واحد بحيث يكون الأبوين هل يقوم بإلغاء تحميل عنصر تحكم المستخدم الحالي وتحميل عنصر تحكم آخر، بدلاً من ظهور مثيل آخر للتطبيق كما هو الحال الآن؟

هل كانت مفيدة؟

المحلول

تنبثق النافذة الجديدة بوضوح لأنك تقوم بإنشاء كائن Shell جديد في كل مرة يتم فيها استدعاء OnSave.أنت بحاجه إلى احصل على نافذة Shell الأصلية.

Shell parentShell = Window.GetWindow(this) as Shell;
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top