Compare commits
2 Commits
187c8de6d3
...
4961ced384
| Author | SHA1 | Date | |
|---|---|---|---|
| 4961ced384 | |||
| 2dfc898e8a |
@@ -1,9 +1,9 @@
|
|||||||
using Microsoft.Extensions.DependencyInjection;
|
namespace RelayClient;
|
||||||
|
|
||||||
namespace RelayClient;
|
|
||||||
|
|
||||||
public partial class App : Application
|
public partial class App : Application
|
||||||
{
|
{
|
||||||
|
private bool _openedSecondWindow;
|
||||||
|
|
||||||
public App()
|
public App()
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
@@ -11,6 +11,28 @@ public partial class App : Application
|
|||||||
|
|
||||||
protected override Window CreateWindow(IActivationState? activationState)
|
protected override Window CreateWindow(IActivationState? activationState)
|
||||||
{
|
{
|
||||||
return new Window(new AppShell());
|
var keeperWindow = new Window(new MainPage("Keeper317"))
|
||||||
|
{
|
||||||
|
Title = "Relay Client - Keeper317"
|
||||||
|
};
|
||||||
|
|
||||||
|
keeperWindow.Created += KeeperWindow_Created;
|
||||||
|
|
||||||
|
return keeperWindow;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void KeeperWindow_Created(object? sender, EventArgs e)
|
||||||
|
{
|
||||||
|
if (_openedSecondWindow)
|
||||||
|
return;
|
||||||
|
|
||||||
|
_openedSecondWindow = false;
|
||||||
|
|
||||||
|
var kiraWindow = new Window(new MainPage("Ru_Kira"))
|
||||||
|
{
|
||||||
|
Title = "Relay Client - Ru_Kira"
|
||||||
|
};
|
||||||
|
|
||||||
|
Current?.OpenWindow(kiraWindow);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
25
RelayClient/ChatSimulator.cs
Normal file
25
RelayClient/ChatSimulator.cs
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
namespace RelayClient;
|
||||||
|
|
||||||
|
public static class ChatSimulator
|
||||||
|
{
|
||||||
|
public static event Action<ChatMessage>? MessageSent;
|
||||||
|
|
||||||
|
public static void Send(string senderUsername, string text)
|
||||||
|
{
|
||||||
|
var message = new ChatMessage
|
||||||
|
{
|
||||||
|
SenderUsername = senderUsername,
|
||||||
|
Text = text,
|
||||||
|
Timestamp = DateTime.Now
|
||||||
|
};
|
||||||
|
|
||||||
|
MessageSent?.Invoke(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public sealed class ChatMessage
|
||||||
|
{
|
||||||
|
public required string SenderUsername { get; set; }
|
||||||
|
public required string Text { get; set; }
|
||||||
|
public required DateTime Timestamp { get; set; }
|
||||||
|
}
|
||||||
@@ -1,36 +1,40 @@
|
|||||||
<?xml version="1.0" encoding="utf-8" ?>
|
<?xml version="1.0" encoding="utf-8" ?>
|
||||||
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
|
<ContentPage
|
||||||
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
|
x:Class="RelayClient.MainPage"
|
||||||
x:Class="RelayClient.MainPage">
|
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
|
||||||
|
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
|
||||||
|
Title="Relay Client">
|
||||||
|
|
||||||
<ScrollView>
|
<Grid RowDefinitions="Auto,*,Auto" Padding="12" RowSpacing="10">
|
||||||
<VerticalStackLayout
|
|
||||||
Padding="30,0"
|
|
||||||
Spacing="25">
|
|
||||||
<Image
|
|
||||||
Source="dotnet_bot.png"
|
|
||||||
HeightRequest="185"
|
|
||||||
Aspect="AspectFit"
|
|
||||||
SemanticProperties.Description="dot net bot in a submarine number ten" />
|
|
||||||
|
|
||||||
<Label
|
<Border Grid.Row="0" StrokeThickness="1" Padding="10">
|
||||||
Text="Hello, World!"
|
<VerticalStackLayout Spacing="4">
|
||||||
Style="{StaticResource Headline}"
|
<Label x:Name="UserLabel"
|
||||||
SemanticProperties.HeadingLevel="Level1" />
|
Text="Logged in as: Unknown"
|
||||||
|
FontAttributes="Bold"
|
||||||
|
FontSize="18" />
|
||||||
|
<Label Text="#general"
|
||||||
|
FontSize="14" />
|
||||||
|
</VerticalStackLayout>
|
||||||
|
</Border>
|
||||||
|
|
||||||
<Label
|
<Border Grid.Row="1" StrokeThickness="1" Padding="10">
|
||||||
Text="Welcome to .NET Multi-platform App UI"
|
<ScrollView x:Name="MessagesScrollView">
|
||||||
Style="{StaticResource SubHeadline}"
|
<VerticalStackLayout x:Name="MessagesLayout" Spacing="8" />
|
||||||
SemanticProperties.HeadingLevel="Level2"
|
</ScrollView>
|
||||||
SemanticProperties.Description="Welcome to dot net Multi platform App U I" />
|
</Border>
|
||||||
|
|
||||||
<Button
|
<Grid Grid.Row="2" ColumnDefinitions="*,Auto" ColumnSpacing="10">
|
||||||
x:Name="CounterBtn"
|
<Entry x:Name="MessageEntry"
|
||||||
Text="Click me"
|
Grid.Column="0"
|
||||||
SemanticProperties.Hint="Counts the number of times you click"
|
Placeholder="Type a message..."
|
||||||
Clicked="OnCounterClicked"
|
ReturnType="Send"
|
||||||
HorizontalOptions="Fill" />
|
Completed="MessageEntry_OnCompleted" />
|
||||||
</VerticalStackLayout>
|
|
||||||
</ScrollView>
|
|
||||||
|
|
||||||
|
<Button Grid.Column="1"
|
||||||
|
Text="Send"
|
||||||
|
Clicked="SendButton_OnClicked" />
|
||||||
|
</Grid>
|
||||||
|
|
||||||
|
</Grid>
|
||||||
</ContentPage>
|
</ContentPage>
|
||||||
@@ -2,22 +2,92 @@
|
|||||||
|
|
||||||
public partial class MainPage : ContentPage
|
public partial class MainPage : ContentPage
|
||||||
{
|
{
|
||||||
int count = 0;
|
private readonly string _username;
|
||||||
|
|
||||||
public MainPage()
|
public MainPage(string username)
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
InitializeComponent();
|
||||||
|
|
||||||
|
_username = username;
|
||||||
|
UserLabel.Text = $"Logged in as: {_username}";
|
||||||
|
|
||||||
|
ChatSimulator.MessageSent += OnMessageSent;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnCounterClicked(object? sender, EventArgs e)
|
private void SendButton_OnClicked(object? sender, EventArgs e)
|
||||||
{
|
{
|
||||||
count++;
|
SendMessage();
|
||||||
|
}
|
||||||
|
|
||||||
if (count == 1)
|
private void MessageEntry_OnCompleted(object? sender, EventArgs e)
|
||||||
CounterBtn.Text = $"Clicked {count} time";
|
{
|
||||||
else
|
SendMessage();
|
||||||
CounterBtn.Text = $"Clicked {count} times";
|
}
|
||||||
|
|
||||||
SemanticScreenReader.Announce(CounterBtn.Text);
|
private void SendMessage()
|
||||||
|
{
|
||||||
|
var text = MessageEntry.Text?.Trim();
|
||||||
|
|
||||||
|
if (string.IsNullOrWhiteSpace(text))
|
||||||
|
return;
|
||||||
|
|
||||||
|
ChatSimulator.Send(_username, text);
|
||||||
|
|
||||||
|
Console.WriteLine($"[{_username}] sent message: {text}");
|
||||||
|
|
||||||
|
MessageEntry.Text = string.Empty;
|
||||||
|
MessageEntry.Focus();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void OnMessageSent(ChatMessage message)
|
||||||
|
{
|
||||||
|
MainThread.BeginInvokeOnMainThread(async () =>
|
||||||
|
{
|
||||||
|
bool isOwnMessage = message.SenderUsername == _username;
|
||||||
|
|
||||||
|
var bubble = new Border
|
||||||
|
{
|
||||||
|
StrokeThickness = 1,
|
||||||
|
Padding = 10,
|
||||||
|
Margin = isOwnMessage
|
||||||
|
? new Thickness(40, 0, 0, 0)
|
||||||
|
: new Thickness(0, 0, 40, 0),
|
||||||
|
HorizontalOptions = isOwnMessage
|
||||||
|
? LayoutOptions.End
|
||||||
|
: LayoutOptions.Start,
|
||||||
|
Content = new VerticalStackLayout
|
||||||
|
{
|
||||||
|
Spacing = 2,
|
||||||
|
Children =
|
||||||
|
{
|
||||||
|
new Label
|
||||||
|
{
|
||||||
|
Text = message.SenderUsername,
|
||||||
|
FontAttributes = FontAttributes.Bold,
|
||||||
|
FontSize = 12
|
||||||
|
},
|
||||||
|
new Label
|
||||||
|
{
|
||||||
|
Text = message.Text,
|
||||||
|
FontSize = 14
|
||||||
|
},
|
||||||
|
new Label
|
||||||
|
{
|
||||||
|
Text = message.Timestamp.ToString("h:mm tt"),
|
||||||
|
FontSize = 10
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
MessagesLayout.Children.Add(bubble);
|
||||||
|
await MessagesScrollView.ScrollToAsync(MessagesLayout, ScrollToPosition.End, true);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
protected override void OnDisappearing()
|
||||||
|
{
|
||||||
|
ChatSimulator.MessageSent -= OnMessageSent;
|
||||||
|
base.OnDisappearing();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user