WPF NET。 3.5 C#对象引用未设置为对象的实例。我该如何解决?

时间:2017-04-21 12:02:02

标签: c# .net wpf xaml runtime-error

我在WPF中用C#NET 3.5编写了一个项目,它在调试时启动得非常好。 对影响" WPF"的所有事物都处于低水平。我希望我能在这个项目中成功告诉我,我成功了。 我无法完全向您发送RootWindow.xaml.cs,因为它有超过9000行并且可能很复杂。

无论如何,当我点击项目中的按钮时出现问题, Visual Studio 会将我发回给告诉我null的行,但我不知道如何解决此问题根据我的研究,非常频繁的错误。

以下是有问题的错误:

  

未处理的类型' System.NullReferenceException'发生在EWIN_THEME_MAKER.exe中。

     

附加信息:未将对象引用设置为对象的实例。

因此,当我点击按钮时,VS会突出显示名为null的对象,所以这是:

 public void StartPreview_Theme()
    {
        this.setTheme();
        this.previewMaker.MakePreview(ref this.theme);
    }
  

this.previewMaker = null

然而,在课程开始时RootWindow.xaml.cs

,我的课程很好
    private PreviewMaker previewMaker;

所以我在我的PreviewMaker课程中进行了搜索,但我看不出可能是null。特别是因为它看起来很复杂,因为最后引用了几个类。有:PreviewMakerThemeLayoutManager

在我的PreviewMaker中,此代码对应于单击按钮时发生的操作:

 public void MakePreview(ref Theme theme)
    {
        this.layoutManager.rootWindow = this.rootWindow;
        this.layoutManager.setThemeInfo(theme);
        if (this.layoutManager.changeLayout())
        {
            this.ConvertLayout(ref theme);
            this.ReloadPreviewImage();
            theme.IsFirstConvert = false;
            theme.setChangeFix();
        }
        else
        {
            MessageBox.Show(this.rootWindow, Resources.Dialog_Err_Convert, Resources.Dialog_Err_Header);
        }
    }

我还指出此行中有一个错误,即null

MessageBox.Show(this.rootWindow, Resources.Dialog_Err_Convert, Resources.Dialog_Err_Header);

这个问题可能来自哪里?

我不会发送我的课程,因为它们太长了,但如果有必要,我可以把它放在它可以帮助我并帮助他人。

PreviewMaker.cs:

    using System;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows;
using EWIN_THEME_MAKER.Properties;
using EWIN_THEME_MAKER.ThemeMaker.Views;

namespace EWIN_THEME_MAKER.ThemeMaker
{
    public class PreviewMaker
    {
        private static readonly IntPtr HWND_TOP = new IntPtr(0);
        private static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
        private LayoutManager layoutManager = new LayoutManager();
        private const uint SWP_NOSIZE = 1;
        private const uint SWP_NOMOVE = 2;
        private const int WM_QUIT = 18;
        private const int WM_COPYDATA = 74;
        private string convertPath;
        private string lytviewerPath;
        private string layoutFolderPath;
        private string layoutDName;
        private string layoutDPlayName;
        private string layoutUName;
        private string layoutUPlayName;
        private string outputDFolderPath;
        private string outputDPlayFolderPath;
        private string outputUFolderPath;
        private string outputUPlayFolderPath;
        public RootWindow rootWindow;
        private IntPtr hWnd;

        public PreviewMaker()
        {
            this.convertPath =  ThemeBinaryConverter.App.tmakeRootPath + "tools\\LayoutConverter\\NW4C_LayoutConverter.exe";
            this.lytviewerPath =  ThemeBinaryConverter.App.tmakeRootPath + "tools\\lytviewer\\lytviewer.exe";
            this.layoutFolderPath =  ThemeBinaryConverter.App.tmakeRootPath + "resources\\clyt\\Menu\\layout_out\\";
            this.layoutDName = this.layoutFolderPath + "DummyMenu_D_00";
            this.layoutDPlayName = this.layoutFolderPath + "DummyMenu_D_00_play";
            this.layoutUName = this.layoutFolderPath + "DummyMenu_U_00";
            this.layoutUPlayName = this.layoutFolderPath + "DummyMenu_U_00_play";
            string tempPath = Path.GetTempPath();
            this.outputDFolderPath = tempPath + "menu_d";
            this.outputDPlayFolderPath = tempPath + "menu_anim_d";
            this.outputUFolderPath = tempPath + "menu_u";
            this.outputUPlayFolderPath = tempPath + "menu_anim_u";
        }

        private void ExecConvertLayout(string inputCLYTFilePath, string inputCLANFilePath, string outputFolderPath)
        {
            string str = "-u -g --bake-infinity \"" + inputCLYTFilePath + "\" \"" + inputCLANFilePath + "\" \"" + outputFolderPath + "\"";
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            Util.Execute(this.convertPath, str, true);
            stopwatch.Stop();
            Console.Write("{0} コンパイル時間(ms)", (object)outputFolderPath);
            Console.WriteLine(" . . . {0}", (object)stopwatch.ElapsedMilliseconds);
        }

        private void ConvertUpLayout()
        {
            this.ExecConvertLayout(this.layoutUName + ".clyt", this.layoutUName + ".clan", this.outputUFolderPath);
            this.ExecConvertLayout(this.layoutUPlayName + ".clyt", this.layoutUPlayName + ".clan", this.outputUPlayFolderPath);
        }

        private void ConvertDownLayout()
        {
            this.ExecConvertLayout(this.layoutDName + ".clyt", this.layoutDName + ".clan", this.outputDFolderPath);
            this.ExecConvertLayout(this.layoutDPlayName + ".clyt", this.layoutDPlayName + ".clan", this.outputDPlayFolderPath);
        }

        private void ConvertLayout(ref Theme theme)
        {
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            WaitWindow waitWindow = new WaitWindow();
            waitWindow.Owner = Application.Current.MainWindow;
            waitWindow.Show();
            waitWindow.setPercentLabel("Wait:0/2");
            if (theme.IsFirstConvert || theme.IsDownChanged)
                this.ConvertDownLayout();
            waitWindow.setPercentLabel("Wait:1/2");
            if (theme.IsFirstConvert || theme.IsUpChanged)
                this.ConvertUpLayout();
            waitWindow.setPercentLabel("Wait:2/2");
            waitWindow.Close();
            stopwatch.Stop();
            Console.WriteLine("Please Wait(ms) = {0}", (object)stopwatch.ElapsedMilliseconds);
        }

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern uint SendMessage(IntPtr hWnd, int msg, IntPtr wParam, ref PreviewMaker.COPYDATASTRUCT lParam);

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern bool PostMessage(IntPtr hWnd, int Msg, int wParam, int lParam);

        [DllImport("user32.dll", SetLastError = true)]
        public static extern int GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId);

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool SetForegroundWindow(IntPtr hWnd);

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, uint flags);

        [DllImport("user32.dll")]
        private static extern int GetWindowRect(IntPtr hwnd, ref PreviewMaker.RECT lpRect);

        public void OpenPreview()
        {
            this.hWnd = PreviewMaker.FindWindow("TMAKE", (string)null);
            if (!this.hWnd.Equals((object)IntPtr.Zero))
                return;
            try
            {
                new Process()
                {
                    StartInfo = {
                        FileName = this.lytviewerPath,
                        Arguments = (" --upper \"" + this.outputUFolderPath + "\" --lower \"" + this.outputDFolderPath + "\"")
                    }
                }.Start();
                Thread.Sleep(1000);
                this.hWnd = PreviewMaker.FindWindow("TMAKE", (string)null);
            }
            catch (Exception ex)
            {
                int num = (int)MessageBox.Show((Window)this.rootWindow, ex.ToString(), Resources.Dialog_Err_Header);
            }
        }

        public void ActivePreview()
        {
            this.hWnd = PreviewMaker.FindWindow("TMAKE", (string)null);
            if (this.hWnd.Equals((object)IntPtr.Zero))
                return;
            PreviewMaker.SetForegroundWindow(this.hWnd);
        }

        public void ReloadPreviewWithDynamicLayout()
        {
            this.OpenPreview();
            PreviewMaker.COPYDATASTRUCT lParam = new PreviewMaker.COPYDATASTRUCT();
            PreviewMaker.LayoutProtocol layoutProtocol;
            layoutProtocol.Verb = "Open";
            layoutProtocol.Data = " -t upper " + this.outputUPlayFolderPath + " -t lower " + this.outputDPlayFolderPath;
            IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf((object)layoutProtocol));
            Marshal.StructureToPtr((object)layoutProtocol, ptr, false);
            lParam.lpData = ptr;
            lParam.cbData = 2112U;
            PreviewMaker.SendMessage(this.hWnd, 74, this.hWnd, ref lParam);
        }

        public void ReloadPreviewImage()
        {
            this.OpenPreview();
            PreviewMaker.COPYDATASTRUCT lParam = new PreviewMaker.COPYDATASTRUCT();
            PreviewMaker.LayoutProtocol layoutProtocol;
            layoutProtocol.Verb = "Open";
            layoutProtocol.Data = " -t upper " + this.outputUFolderPath + " -t lower " + this.outputDFolderPath;
            IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf((object)layoutProtocol));
            Marshal.StructureToPtr((object)layoutProtocol, ptr, false);
            lParam.lpData = ptr;
            lParam.cbData = 2112U;
            PreviewMaker.SendMessage(this.hWnd, 74, this.hWnd, ref lParam);
        }

        public void ChangePreviewParam(string paramData)
        {
            this.hWnd = PreviewMaker.FindWindow("TMAKE", (string)null);
            if (this.hWnd.Equals((object)IntPtr.Zero))
                this.ReloadPreviewImage();
            PreviewMaker.COPYDATASTRUCT lParam = new PreviewMaker.COPYDATASTRUCT();
            PreviewMaker.LayoutProtocol layoutProtocol;
            layoutProtocol.Verb = "Change";
            layoutProtocol.Data = paramData;
            IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf((object)layoutProtocol));
            Marshal.StructureToPtr((object)layoutProtocol, ptr, false);
            lParam.lpData = ptr;
            lParam.cbData = 2112U;
            PreviewMaker.SendMessage(this.hWnd, 74, this.hWnd, ref lParam);
            int lpdwProcessId;
            PreviewMaker.GetWindowThreadProcessId(this.hWnd, out lpdwProcessId);
            Interaction.AppActivate(lpdwProcessId);
        }

        public void ChangePreviewAnimationParam(int animParam)
        {
            this.ChangePreviewParam(" -anim " + (object)animParam);
        }

        public void ChangePreviewVisiblePaneParam(string paneName)
        {
            this.ChangePreviewParam(" -panevisible N_BaseCtr_00");
        }

        public void ChangePreviewPositionPaneParam(string paneName, int x, int y)
        {
            this.ChangePreviewParam(" -panetrans " + paneName + " " + (object)x + " " + (object)y);
        }

        public void MakePreview(ref Theme theme)
        {
            this.layoutManager.rootWindow = this.rootWindow;
            this.layoutManager.setThemeInfo(theme);
            if (this.layoutManager.changeLayout())
            {
                this.ConvertLayout(ref theme);
                this.ReloadPreviewImage();
                theme.IsFirstConvert = false;
                theme.setChangeFix();
            }
            else
            {
                int num = (int)MessageBox.Show((Window)this.rootWindow, Resources.Dialog_Err_Convert, Resources.Dialog_Err_Header);
            }
        }

        public void ClosePreview()
        {
            this.hWnd = PreviewMaker.FindWindow("TMAKE", (string)null);
            if (this.hWnd.Equals((object)IntPtr.Zero))
                return;
            PreviewMaker.RECT lpRect = new PreviewMaker.RECT();
            PreviewMaker.GetWindowRect(this.hWnd, ref lpRect);
            Settings.Default.lytviewerX = (double)lpRect.left;
            Settings.Default.lytviewerY = (double)lpRect.top;
            PreviewMaker.PostMessage(this.hWnd, 18, 0, 0);
        }

        public void CaptureReportImage(ref Theme theme, string captureFolderPath)
        {
            int num = theme.RecommendRowNum < eRecommendRowType.Row1 || theme.RecommendRowNum > eRecommendRowType.Row6 ? 11 : 5 + 7 * (int)(theme.RecommendRowNum - 1) + 6;
            this.ClosePreview();
            string str = "" + " --upper \"" + this.outputUFolderPath + "\"" + " --lower \"" + this.outputDFolderPath + "\"" + " --screenshot " + (object)num + " 0 \"" + captureFolderPath + "\"";
            Process process = new Process();
            process.StartInfo.FileName = this.lytviewerPath;
            process.StartInfo.Arguments = str;
            process.Start();
            process.WaitForExit();
            this.hWnd = PreviewMaker.FindWindow("TMAKE", (string)null);
        }

        public void Capture(string captureFolderPath)
        {
            this.Capture(captureFolderPath, 0);
        }

        public void Capture(string captureFolderPath, int animationNum)
        {
            this.ClosePreview();
            string str1 = "";
            if (Settings.Default.CaptureTypeStr == "Both")
                str1 = str1 + " --upper \"" + this.outputUFolderPath + "\"" + " --lower \"" + this.outputDFolderPath + "\"";
            else if (Settings.Default.CaptureTypeStr == "Up")
                str1 = str1 + " --upper \"" + this.outputUFolderPath + "\"";
            else if (Settings.Default.CaptureTypeStr == "Down")
                str1 = str1 + " --lower \"" + this.outputDFolderPath + "\"";
            string str2 = str1 + " --screenshot " + (object)animationNum + " 0 \"" + captureFolderPath + "\"";
            Process process = new Process();
            process.StartInfo.FileName = this.lytviewerPath;
            process.StartInfo.Arguments = str2;
            process.Start();
            process.WaitForExit();
            this.hWnd = PreviewMaker.FindWindow("TMAKE", (string)null);
        }

        private struct RECT
        {
            public int left;
            public int top;
            public int right;
            public int bottom;
        }

        public struct COPYDATASTRUCT
        {
            public IntPtr dwData;
            public uint cbData;
            public IntPtr lpData;
        }

        public struct LayoutProtocol
        {
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
            public string Verb;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1024)]
            public string Data;
        }
    }
}

2 个答案:

答案 0 :(得分:2)

确保您创建了PreviewMaker类的实例,即尝试在RootWindow.xaml.cs中更改此行:

private PreviewMaker previewMaker;

......对此:

private PreviewMaker previewMaker = new PreviewMaker();

答案 1 :(得分:1)

您已声明previewMaker,但在尝试访问之前未初始化

换句话说,

private PreviewMaker previewMaker;

将导致previewMaker为空 该行不会创建PreviewMaker类型的对象