0

I try to make Visual C# app, which is using UserControl (WPF). By default, VS Community 2015 C# generates UserControl in the same namespace as main program. It's built correctly. I'd like to separate it in it's own namespace for possible future reuse. I change auto-generated code to have UserControl in separate namespace.

Here is my code

UserControl

<UserControl x:Class="nsTabI2CMemRW.TabI2CMemRW"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
        xmlns:local="clr-namespace:nsTabI2CMemRW"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="500">

    <Grid>
        <TextBox x:Name="AddrH"/>
        <Label x:Name="AddrH_Label"/>
    </Grid>

C# class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WpfApplication1;    // This is my main app namespace

namespace nsTabI2CMemRW
{
    /// <summary>
    /// Interaction logic for TabI2CMemRW.xaml
    /// </summary>
    public partial class TabI2CMemRW : UserControl
    {
        public TabI2CMemRW()
        {
            //InitializeComponent();
            ((WpfApplication1.MainWindow)Application.Current.MainWindow).InitializeComponent();  //InitializeComponent();
        }
    }
}

Main Windows XAML

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApplication1"
    xmlns:UserControl="clr-namespace:nsTabI2CMemRW"
    mc:Ignorable="d"
    Title="MainWindow" Height="649.005" Width="620.667" ScrollViewer.CanContentScroll="True">
<Grid>
    <TabControl x:Name="tabControl" Margin="0,0,0.4,0.2">
        <TabControl.BindingGroup>
            <BindingGroup/>
        </TabControl.BindingGroup>
        <TabItem x:Name="I2CMemReadWrite" Header="I2C Mem Read/Write">

            <UserControl:TabI2CMemRW Margin="0,0,-0.2,278.2"/>

        </TabItem>

        <TabItem Header="TabItem">
        <Button x:Name="button" Content="Button" Height="100" Width="75"/>
        </TabItem>

    </TabControl>

</Grid>

Designer shows error "Object reference is not set to an instance of an object" for line

<UserControl:TabI2CMemRW Margin="0,0,-0.2,278.2"/>

Program compiled fine but first tab (where my UserControl should be) is blank.

Fyodor Yemelyanenko
  • 11,264
  • 1
  • 30
  • 38
  • 2
    Changing autogenerated code by hand is usually not a good idea – Fang Nov 10 '16 at 12:19
  • you call the `InitializeComponent` of the `MainWindow` inside the UserControl. This can not work and is more than wrong. And I guess that `Application.Current.MainWindow` is null because you are in the middle of initializing the MainWindow -> it can not exist while you creating it. This is why you end up with a NullRefernence Exception – Mat Nov 10 '16 at 12:57

2 Answers2

1

There are many things wrong here, cant answer them all. try comenting ((WpfApplication1.MainWindow)Application.Current.MainWindow).InitializeComponent();

and leaving

InitializeComponent();

instead in your C# class

  • Yes, this `((WpfApplication1.MainWindow)Application.Current.MainWindow).InitializeComponent();` was wrong! Error "Object reference is not set to an instance of an object" relates to this code. – Fyodor Yemelyanenko Nov 14 '16 at 06:50
0

Yes, this ((WpfApplication1.MainWindow)Application.Current.MainWindow).InitializeComponent(); was wrong! Error "Object reference is not set to an instance of an object" relates to this code (Found using VS debugger as in this post WPF, 'Object reference not set to an instance of an object' in Designer. I think it's useful for similar cases with WPF).

After commenting-out line (see above) and leaving only InitializeComponent() and recompiling code twice - it works! Also I've checked that right code-behind file bound to XAML file (Accidentally deleted the code behind for an Xaml file. How do I add the code behind again?).

And also this lines are important xmlns:UserControl="clr-namespace:nsTabI2CMemRW" and <UserControl:TabI2CMemRW Margin="0,0,-0.2,278.2"/>. I think I can missed them first time. (After xmlns: can be any name, not only UserControl)

Community
  • 1
  • 1
Fyodor Yemelyanenko
  • 11,264
  • 1
  • 30
  • 38