0

WPF アプリケーションがあり、プログラムでコンテンツを表示したいと考えています。しかし、これを特定の順序で実行しようとすると、表示に失敗します。

これは機能します

public partial class MainWindow : Window
    {
        private static GX3Logger logger = GX3Logger.GetLogger();
        private GX3ClientMain.GX3ClientMain clientMain = null;

        /// <summary>
        ///  Main method
        /// </summary>
        /// <param name="args">
        ///  Application args
        /// </param>
        /// 
        public MainWindow()
        {
            InitializeComponent();
            Button btn = new Button();
            btn.Content = "Moo";
            btn.Height = 100;
            btn.Width = 100;
            stackPanel1.Children.Add(btn);
        }



        public int Initialise(string[] args)
        {
        ........
        }
}

これはしません

public partial class App : Application
    {
        void app_Startup(object sender, StartupEventArgs e)
        {
            MainWindow mw = new MainWindow();
            mw.Initialise(e.Args);

        }
    }

public partial class MainWindow : Window
    {
        private static GX3Logger logger = GX3Logger.GetLogger();
        private GX3ClientMain.GX3ClientMain clientMain = null;

        /// <summary>
        ///  Main method
        /// </summary>
        /// <param name="args">
        ///  Application args
        /// </param>
        /// 
        public MainWindow()
        {
            InitializeComponent();            
        }



        public int Initialise(string[] args)
        {
            Button btn = new Button();
            btn.Content = "Moo";
            btn.Height = 100;
            btn.Width = 100;
            stackPanel1.Children.Add(btn);
            .......
        }
}

なぜだめですか?

4

1 に答える 1

1

タブ項目に StackPanel を配置する必要があります

<Window x:Class="WpfApplication1.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" Loaded="Window_Loaded">
        <TabControl>
            <TabItem Name="tab1">
                <StackPanel Name="stackPanel1">

                </StackPanel>
            </TabItem>
        </TabControl>
</Window>

次に、コードビハインドで

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Button btn = new Button();
            btn.Content = "Moo";
            stackPanel1.Children.Add(btn);
            Button btn2 = new Button();
            btn2.Content = "test";
            stackPanel1.Children.Add(btn2);

        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
        }
    }
}
于 2012-05-04T09:24:27.003 に答える