Skip to content

Commit dd635bb

Browse files
committed
添加端口配置和图标支持
在 `LocalDrop.csproj` 中添加了 `LocalDrop.ico` 的嵌入资源支持,并设置了 `CopyToOutputDirectory` 属性。 在 `LocalDrop.json` 中新增 `port` 键,值为 `27431`。 在 `MainWindow.xaml.cs` 中添加了获取当前目录和文件路径的方法,并设置窗口图标。 在 `NavItemReceiver.xaml.cs` 和 `NavItemSend.xaml.cs` 中使用从 JSON 配置读取的端口值替代硬编码的端口号。 在 `NavItemSetting.xaml` 中添加了用于输入和保存端口号的 UI 组件,并在 `NavItemSetting.xaml.cs` 中实现了相关逻辑。 在 `FileReceiver.cs` 中确保在 UI 线程上更新接收状态。
1 parent e90f92a commit dd635bb

8 files changed

Lines changed: 38 additions & 9 deletions

LocalDrop.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@
7777
<EmbeddedResource Include="Assets\avatar.jpg">
7878
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
7979
</EmbeddedResource>
80-
<EmbeddedResource Include="Assets\LocalDrop.ico" />
80+
<EmbeddedResource Include="Assets\LocalDrop.ico">
81+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
82+
</EmbeddedResource>
8183
</ItemGroup>
8284

8385
<ItemGroup>

LocalDrop.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
{
2-
"save_path": "D:\\LocalDrop"
2+
"save_path": "D:\\LocalDrop",
3+
"port": 27431
34
}

MainWindow.xaml.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,21 @@ namespace LocalDrop
99
/// </summary>
1010
public sealed partial class MainWindow : Window
1111
{
12+
private static string GetCurrentDirectory()
13+
{
14+
return System.AppDomain.CurrentDomain.BaseDirectory;
15+
}
1216

17+
public static string GetCurrentFile(string filename)
18+
{
19+
return GetCurrentDirectory() + filename;
20+
}
1321

1422
public MainWindow()
1523
{
1624
this.InitializeComponent();
1725
ContentFrame.Navigate(typeof(NavItemReceiver));
26+
this.AppWindow.SetIcon(GetCurrentFile("Assets\\LocalDrop.ico"));
1827

1928
//ContentFrame.Navigate(typeof(NavItemSend));
2029
}

NavItemReceiver.xaml.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ namespace LocalDrop
1818
{
1919
public sealed partial class NavItemReceiver : Page
2020
{
21+
private int port = int.Parse(MySettings.ReadJsonToDictionary()["port"].ToString());
2122
WiFiDirectAdvertisementPublisher _publisher = new WiFiDirectAdvertisementPublisher();
2223
DeviceWatcher? _deviceWatcher = null;
2324
bool _fWatcherStarted = false;
@@ -209,7 +210,7 @@ private async void ConnectionRequestedHandler(
209210
Debug.WriteLine($"LocalServiceName:{pair.LocalServiceName} RemoteServiceName:{pair.RemoteServiceName}");
210211
Debug.WriteLine($"LocalHostName:{pair.LocalHostName} RemoteHostName:{pair.RemoteHostName}");
211212

212-
await receiver.StartAsync(27431);
213+
await receiver.StartAsync(port);
213214

214215
}
215216
}
@@ -285,10 +286,13 @@ private void SaveHistory()
285286
{
286287
try
287288
{
288-
var localFolder = ApplicationData.Current.LocalFolder;
289-
var historyFile = Path.Combine(localFolder.Path, HistoryFileName);
290-
var json = JsonConvert.SerializeObject(ReceivedFiles);
291-
File.WriteAllText(historyFile, json);
289+
DispatcherQueue.TryEnqueue(() =>
290+
{
291+
var localFolder = ApplicationData.Current.LocalFolder;
292+
var historyFile = Path.Combine(localFolder.Path, HistoryFileName);
293+
var json = JsonConvert.SerializeObject(ReceivedFiles);
294+
File.WriteAllText(historyFile, json);
295+
});
292296
}
293297
catch (Exception ex)
294298
{

NavItemSend.xaml.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public sealed partial class NavItemSend : Page
7070
private ObservableCollection<SendTransferItem> ActiveSendTransfers { get; } = new();
7171
private double _totalProgress = 0;
7272
private string _totalProgressText = "";
73+
private int port = int.Parse(MySettings.ReadJsonToDictionary()["port"].ToString());
7374

7475
public double TotalProgress
7576
{
@@ -552,7 +553,7 @@ private async System.Threading.Tasks.Task<String> Connect(string deviceId)
552553

553554
await fileSender.SendFileAsync(fileInfo,
554555
endpointPair.RemoteHostName.ToString(),
555-
27431,
556+
port,
556557
transfer);
557558

558559
DispatcherQueue.TryEnqueue(() =>

NavItemSetting.xaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919
Text=""/>
2020
<Button Content="浏览..." Width="80" Height="30" Click="BrowseFolderButton_Click"/>
2121
</StackPanel>
22+
<StackPanel Orientation="Horizontal" Spacing="10" VerticalAlignment="Center">
23+
<TextBlock Text="端口:" VerticalAlignment="Center" FontSize="16"/>
24+
<TextBox x:Name="SocketTextBox" Width="300" Height="30"
25+
Text=""/>
26+
<Button Content="保存" Width="80" Height="30" Click="SocketButton_Click"/>
27+
</StackPanel>
2228
<StackPanel Orientation="Horizontal" Spacing="15">
2329
<!-- GitHub 按钮 -->
2430
<Button Content="GitHub 主页"

NavItemSetting.xaml.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public NavItemSetting()
2121
DispatcherQueue.TryEnqueue(() =>
2222
{
2323
SavePathTextBox.Text = MySettings.ReadJsonToDictionary()["save_path"].ToString();
24+
SocketTextBox.Text = MySettings.ReadJsonToDictionary()["port"].ToString();
2425
});
2526
}
2627
private async void BrowseFolderButton_Click(object sender, RoutedEventArgs e)
@@ -44,6 +45,12 @@ private async void BrowseFolderButton_Click(object sender, RoutedEventArgs e)
4445
SavePathTextBox.Text = folder.Path;
4546
});
4647
}
48+
private void SocketButton_Click(object sender, RoutedEventArgs e)
49+
{
50+
var settings = MySettings.ReadJsonToDictionary();
51+
settings["port"] = SocketTextBox.Text;
52+
MySettings.SaveDictionaryToJson(settings);
53+
}
4754

4855
private void GitHubButton_Click(object sender, RoutedEventArgs e)
4956
{

Receiver/FileReceiver.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,6 @@ private async Task ProcessFileTransfer(StreamSocket socket)
147147
}
148148
DispatcherQueue.TryEnqueue(() =>
149149
{
150-
151150
transferItem.Status = TransferReceivingStatus.Completed;
152151
FileTransferCompleted?.Invoke(transferItem);
153152
});

0 commit comments

Comments
 (0)