Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Windows 7+ JumpList support #43

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 75 additions & 2 deletions Bililive_dm/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Navigation;
using System.Windows.Shell;
using Microsoft.Win32;
using Newtonsoft.Json.Linq;
using Application = System.Windows.Application;
Expand Down Expand Up @@ -113,7 +114,19 @@ public MainWindow()

try
{
this.RoomId.Text = Properties.Settings.Default.roomId.ToString();
SyncJumpList();
string arg = Environment.CommandLine;
if (arg.Contains("RoomID="))
{
int start = arg.LastIndexOf("RoomID=") + 7;
int end = Math.Min(arg.IndexOf(' ', start), arg.IndexOf(',', start)) - 1;
if (end == -2) end = arg.Length;
this.RoomId.Text = arg.Substring(start, end - start);
}
else
{
this.RoomId.Text = Properties.Settings.Default.roomId.ToString();
}
}
catch
{
Expand All @@ -135,7 +148,7 @@ public MainWindow()
var seconds = int.Parse(fullversion.Split('.')[3]);
dt = dt.AddDays(dates);
dt = dt.AddSeconds(seconds*2);
if (ApplicationDeployment.IsNetworkDeployed)
if (ApplicationDeployment.IsNetworkDeployed || Environment.CommandLine.Contains("RoomID="))
{
Title += Properties.Resources.MainWindow_MainWindow____版本号__ +
ApplicationDeployment.CurrentDeployment.CurrentVersion;
Expand Down Expand Up @@ -583,6 +596,7 @@ private async void connbtn_Click(object sender, RoutedEventArgs e)
SendSSP(Properties.Resources.MainWindow_connbtn_Click_連接成功);
Ranking.Clear();
SaveRoomId(roomId);
AddJumpListItem(roomId);

foreach (var dmPlugin in App.Plugins)
{
Expand Down Expand Up @@ -1495,5 +1509,64 @@ private void SelectLanguage(object sender, RoutedEventArgs e)
lg.ShowDialog();

}

private void AddJumpListItem(int roomId)
{
try
{
JumpTask jumpTask = new JumpTask()
{
ApplicationPath = Assembly.GetExecutingAssembly().Location,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think Assembly.GetExecutingAssembly().Location may changed after update

Copy link
Author

@Elepover Elepover Mar 12, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

走 ClickOnce

得想个办法让弹幕姬获取到 ClickOnce 的 .appref-ms 文件

也许可以用 ApplicationDeployment.CurrentDeployment.ActivationUri.ToString() 来替代程序路径(并且同时取消对打开方式检查中“房间号启动”方式的豁免),但因为没有 ClickOnce 调试条件而一时无法继续

以及,如果直接打开 .appref-ms 的话,参数传递也是个问题

另一个思路

应用可以读取并修改自己已有的 JumpList, 所以也可以尝试在新版本启动时检测检测现有的 JumpList 中的项目并逐个修正

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

添加了可执行文件名变动修复

IconResourcePath = Assembly.GetExecutingAssembly().Location,
IconResourceIndex = 1,
Title = roomId.ToString(),
Description = $"连接房间 {roomId}",
CustomCategory = "最近房间",
Arguments = $"RoomID={roomId}"
};

bool newJumpList = false;
JumpList jumpList = JumpList.GetJumpList(App.Current);
if (jumpList == null)
{
// 设置新的 JumpList
jumpList = new JumpList();
newJumpList = true;
}
else
{
// 搜索是否重复
foreach (JumpTask task in jumpList.JumpItems)
{
if (task.Title == roomId.ToString()) return;
}
}

while (jumpList.JumpItems.Count >= 10)
{
jumpList.JumpItems.RemoveAt(0);
}

jumpList.JumpItems.Add(jumpTask);
if (newJumpList) JumpList.SetJumpList(App.Current, jumpList);
else jumpList.Apply();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

private void SyncJumpList()
{
JumpList jumpList = JumpList.GetJumpList(App.Current);
if (jumpList == null) return;
string path = Assembly.GetExecutingAssembly().Location;
foreach (JumpTask task in jumpList.JumpItems)
{
task.ApplicationPath = path;
}
jumpList.Apply();
}
}
}