【发布时间】:2021-08-23 14:21:24
【问题描述】:
我制作了一个提示器,黑色背景上的白色文本滚动到结束。收到的错误不会破坏程序,但它们是可疑的。
我明白了:
警告 RichTextBox, Name='RichPrompterTextBox' Text.Length TextBlock.Text, Name='CharacterCounterTextBlock' String 上找不到文本属性 RichTextBox 类型的对象。警告 RichTextBox, Name='RichPrompterTextBox' MaxLength TextBlock.Text, Name='CharacterCounterTextBlock' 未找到字符串 MaxLength 属性 在 RichTextBox 类型的对象上。错误空(0)TextBlock.Visibility, Name='CharacterCounterTextBlock' Visibility 找不到源: 相对源查找祖先, AncestorType='System.Windows.Controls.TextBox', AncestorLevel='1'。
我有这个 XAML:
<Window x:Class="WpfDrawing.Prompter.PrompterView"
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:WpfDrawing.View"
xmlns:prompter="clr-namespace:WpfDrawing.Prompter" d:DataContext="{d:DesignInstance Type=prompter:PrompterViewModel}"
mc:Ignorable="d"
WindowState="Maximized"
WindowStyle="None"
Title="Prompter" Height="450" Width="800">
<RichTextBox
Background="Black"
Name ="prompterRichTextBox"
Foreground="White">
</RichTextBox>
</Window>
还有这个.cs:
/// <summary>
/// Interaction logic for Prompter.xaml
/// </summary>
public partial class PrompterView : Window
{
private readonly PrompterViewModel vm;
public PrompterView(PrompterViewModel vm)
{
this.vm = vm;
DataContext = vm;
InitializeComponent();
this.vm.PropertyChanged += Vm_PropertyChanged;
this.RichPrompterTextBox.MouseWheel += RichPrompterTextBox_MouseWheel;
this.RichPrompterTextBox.LayoutUpdated += RichPrompterTextBox_LayoutUpdated;
this.RichPrompterTextBox.TextChanged += RichPrompterTextBox_TextChanged;
SetToAdditionalMonitor();
}
private void SetToAdditionalMonitor()
{
var screens = Screen.AllScreens;
for (var i = 0; i != screens.Length; i++)
{
if (!screens[i].Primary)
{
this.WindowState = WindowState.Normal;
this.WindowStartupLocation = WindowStartupLocation.Manual;
this.Left = screens[i].Bounds.Left;
this.Top = screens[i].Bounds.Top;
this.Width = screens[i].Bounds.Width;
this.Height = screens[i].Bounds.Height;
}
}
}
private void RichPrompterTextBox_TextChanged(object sender, TextChangedEventArgs e)
{
var textRange = new TextRange(this.RichPrompterTextBox.Document.ContentStart, this.RichPrompterTextBox.Document.ContentEnd);
vm.Text = textRange.Text;
}
private void RichPrompterTextBox_LayoutUpdated(object sender, System.EventArgs e)
{
vm.Height = this.RichPrompterTextBox.ViewportHeight + this.RichPrompterTextBox.ExtentHeight;
}
private void RichPrompterTextBox_MouseWheel(object sender, MouseWheelEventArgs e)
{
vm.ScrollPosition = RichPrompterTextBox.VerticalOffset;
}
private void Vm_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(vm.ScrollPosition))
{
this.RichPrompterTextBox.Dispatcher.Invoke(() => this.RichPrompterTextBox.ScrollToVerticalOffset(vm.ScrollPosition));
}
if(e.PropertyName == nameof(vm.Text))
{
var textRange = new TextRange(this.RichPrompterTextBox.Document.ContentStart, this.RichPrompterTextBox.Document.ContentEnd);
if (textRange.Text != vm.Text)
{
this.RichPrompterTextBox.Document = new FlowDocument(new Paragraph(new Run(vm.Text)));
}
}
}
private void Reset(object sender, RoutedEventArgs e)
{
vm.Reset();
}
private void StartStop(object sender, RoutedEventArgs e)
{
vm.RunStop();
}
private void CloseWindow(object sender, RoutedEventArgs e) => Close();
}
Xaml 错误: errors
我没有绑定任何东西,但出现绑定错误。
【问题讨论】:
-
您的代码无法编译,
this.RichPrompterTextBox应该是this.prompterRichTextBox吗?还提供您的 ViewModel 吗?