我今天尝试打开Direct协议v4.00,但是我遇到了IP地址处理的障碍。一些客户端IP地址是IPv6地址,例如:<Window x:Class="ButtonContentExample.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:ButtonContentExample"
mc:Ignorable="d"
Title="MainWindow"
Height="450"
Width="800">
<Window.DataContext>
<local:ExampleViewModel />
</Window.DataContext>
<StackPanel>
<TextBlock Text="{Binding TestResult}"
Margin="12"
FontSize="20" />
<ListBox ItemsSource="{Binding Items}">
<ItemsControl.ItemTemplate>
<DataTemplate DataType="{x:Type local:TestItemViewModel}">
<Button Content="{Binding Value}"
Command="{Binding TestCommand}"
CommandParameter="{Binding Content, RelativeSource={RelativeSource Mode=Self}}"
Width="50"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ListBox>
</StackPanel>
(我对该地址进行了一些调整)
当我通过发送这些IPv6地址时,出现以下错误:
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows.Input;
namespace ButtonContentExample
{
public class RelayCommand : ICommand
{
public event EventHandler CanExecuteChanged;
public RelayCommand(Action<object> execute) : this(execute, null) { }
public RelayCommand(Action<object> execute, Predicate<object> canExecute)
{
ExecuteAction = execute;
CanExecutePredicate = canExecute ?? new Predicate<object>(obj => true);
}
public Action<object> ExecuteAction { get; }
public Predicate<object> CanExecutePredicate { get; }
public bool CanExecute(object parameter) => CanExecutePredicate(parameter);
public void Execute(object parameter) => ExecuteAction(parameter);
}
public abstract class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void Notify<T>(ref T t, T value, [CallerMemberName] string name = "")
{
t = value;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
}
public class ExampleViewModel : ViewModelBase
{
private string testResult;
private ObservableCollection<TestItemViewModel> items = new ObservableCollection<TestItemViewModel>();
public ExampleViewModel()
{
for (var i = 0; i < 50; i++) Items.Add(new TestItemViewModel(this) { Value = i });
}
public ObservableCollection<TestItemViewModel> Items
{
get => items;
set => Notify(ref items, value);
}
public string TestResult
{
get => testResult;
set => Notify(ref testResult, value);
}
}
public class TestItemViewModel : ViewModelBase
{
private int value;
private string result;
public TestItemViewModel(ExampleViewModel parent)
{
Parent = parent;
TestCommand = new RelayCommand(obj => Result = obj.ToString());
}
public int Value
{
get => value;
set => Notify(ref this.value, value);
}
public string Result
{
get { return result; }
set
{
Notify(ref result, value);
Parent.TestResult = value;
}
}
public RelayCommand TestCommand { get; }
public ExampleViewModel Parent { get; }
}
}
如果我完全省略了IP地址,则会出现以下错误:
2a02:c7f:d192:dc00:de4:a84d:aab5:8225
那么,当我收到一个IPv6地址时我打算做什么?退回直接协议v3.00?