简体   繁体   English

如何用自定义窗口替换WPF标准窗口?

[英]How do I replace the WPF standard window with a custom window?

Right now I'm trying to create a voice recognition assistant (basically a JARVIS program) using c#. 现在,我正在尝试使用c#创建语音识别助手(基本上是JARVIS程序)。 I started off writing the code in visual studio, and once the code was working, I moved over to expression blend to work on the UI. 我开始在Visual Studio中编写代码,一旦代码开始工作,我便移至表达式混合以在UI上工作。

I've successfully imported the images and animated them on a loop. 我已经成功导入了图像并对其进行了循环动画处理。 At that point, when I ran the program, everything worked (animations and speech recognition included). 到那时,当我运行程序时,一切正常(包括动画和语音识别)。 My next step was then to try and "replace" the standard window with the images I had just imported by setting the standard window to transparent. 然后,我的下一步是通过将标准窗口设置为透明来尝试用刚导入的图像“替换”标准窗口。 Using the panes, I then set all the brushes to "no brush," and I set allowstransparency to "true." 然后,使用窗格将所有画笔设置为“无画笔”,并将allowtransparency设置为“ true”。 When I ran the program, the animations still worked but the program stopped running all of the voice recognition code. 当我运行该程序时,动画仍然有效,但是该程序停止运行所有语音识别代码。

Through the process of elimination, I've isolated the problem to the background brush and the allowstransparency function. 通过消除的过程,我将问题隔离到了背景画笔和allowtransparency函数中。 If I either set the allowstransparency to true or mess with the background brush in any way, there's no more speech recognition. 如果我将allowtransparency设置为true或以任何方式使背景画笔弄乱了,就没有语音识别了。

I'm new to coding, so the only reason I can think of (and I don't even know if this is the reason) is that the speech recognition directory and functions are mapped to the window. 我是编码的新手,所以我能想到的唯一原因(而且我什至不知道这是否是原因)是语音识别目录和功能已映射到窗口。

Is there any reason for why this is happening/is there a fix? 有什么理由为什么会发生这种情况/是否有解决方法?


EDIT: Code added 编辑:添加代码

    public MainWindow()
    {
        InitializeComponent();
        Loaded += MainWindow_Loaded;
    }

    private void MainWindow_Loaded(object sender, EventArgs e)
    {
        _recognizer.SetInputToDefaultAudioDevice();
        _recognizer.LoadGrammar(new Grammar(new GrammarBuilder(new Choices(File.ReadAllLines(@"C:\users\username\Documents\commands.txt")))));
        _recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(_recognizer_SpeechRecognized);
        _recognizer.RecognizeAsync(RecognizeMode.Multiple);
    }
    void _recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
    {
        int ranNum = rnd.Next(1, 10);
        string speech = e.Result.Text;
        switch (speech)
        {
            //INTERACTIONS
            case "hello":

...and so on ...等等

You need to complete four steps to make your window "Disappear": 您需要完成四个步骤,以使窗口“消失”:

  1. Window.WindowStyle set equal to "None." Window.WindowStyle设置为等于“无”。
  2. Window.Background set equal to "Transparent" Window.Background设置为等于“透明”
  3. Window.BorderThickness set equal to "0" Window.BorderThickness设置为等于“ 0”
  4. Window.AllowsTransparency set equal to "True" Window.AllowsTransparency设置为等于“ True”

I see no reason for your speech recognition to be tied to the Window appearance, but you need to post some code; 我认为您的语音识别功能与Window外观无关,但是您需要发布一些代码。 initialization code, and some example usage would probably be very useful in diagnosing the rest of the issue. 初始化代码以及一些示例用法在诊断其余问题时可能非常有用。

If you need the user to click on pieces of the user interface, you may need to set the Background to an "almost transparent" color, instead of a pure transparent brush. 如果需要用户单击用户界面的各个部分,则可能需要将“背景”设置为“几乎透明”的颜色,而不是纯透明的画笔。 The reason for this is; 原因是; when a background is purely "Transparent," it no longer detects click events - it treats the background as though it is no longer there. 当背景纯粹是“透明的”时,它将不再检测点击事件-它将背景视为不再存在。

You could try something like this: 您可以尝试这样的事情:

Background="#01000000"

You will also probably want to keep a reference to the Task which (I assume) is returned by your RecognizeAsync method call; 您可能还需要保留对Task的引用,该Task由我的RecognizeAsync方法调用返回; no telling when that might be inadvertently garbage-collected. 不知道什么时候可能会无意中被垃圾收集。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM