You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#密码管理器无障碍错误:设为public仍存在多处报错

密码管理器代码报错修复方案

以下是你代码中存在的问题及对应的修复步骤:

一、语法与拼写错误

  1. Database类多余闭合括号
    你的Database类末尾多了一个额外的},直接删除即可,否则会触发编译语法错误。
  2. Initialize方法拼写错误
    方法名Inizialize拼写错误,正确应为Initialize,虽然当前内部调用逻辑没问题,但统一修正可避免后续调用时的拼写失误。
  3. List与ObservableCollection变量名冲突
    在LoginCredentialListPage的OnNavigatedTo方法中,你将数据库获取的列表命名为items,随后误写items.Clear(),但实际需要清空的是ObservableCollection类型的Items属性,变量名冲突导致逻辑错误,应改为Items.Clear()。

二、导航逻辑错误

在LoginCredentialListPage的OnSelectionChanged和OnItemAdd方法中,你错误地导航到当前列表页LoginCredentialListPage,实际应该跳转到详情页LoginCredentialItemPage,否则点击列表项或添加按钮无法进入编辑/新增页面。

三、依赖实体类缺失

代码中未包含LoginCredential实体类的定义,SQLite要求实体类必须有public属性,且需标记主键(通常为自增Id),否则CreateTableAsync会报错。你需要补充该类的定义,示例如下:

using SQLite;

public class LoginCredential
{
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }
    public string Website { get; set; }
    public string Username { get; set; }
    public string Password { get; set; }
}

四、修正后的关键代码片段

1. 修正后的Database类

public class Database
{
    public SQLiteAsyncConnection connection;
    public Database()
    {
    }
    public async Task Initialize()
    {
        if (connection is not null)
            return;

        connection = new SQLiteAsyncConnection(Constants.DatabasePath);

        await connection.CreateTableAsync<LoginCredential>();
    } 
    public async Task<List<LoginCredential>> GetItemsAsync()
    {
        await Initialize();
        return await connection.Table<LoginCredential>().ToListAsync();
    }
    public async Task<int> SaveItemsAsync(LoginCredential item)
    {
        await Initialize();

        if (item.Id != 0)
        {
            return await connection.UpdateAsync(item);
        }
        else
        {
            return await connection.InsertAsync(item);
        } 
    }
    public async Task<int> DeleteItemsAsync(LoginCredential item)
    {
        await Initialize();
        return await connection.DeleteAsync(item);
    }
}

2. 修正后的LoginCredentialListPage导航逻辑

public async void OnSelectionChanged(System.Object sender, Microsoft.Maui.Controls.SelectionChangedEventArgs e)
{
    if (e.CurrentSelection.FirstOrDefault() is not LoginCredential item)
        return;

    await Shell.Current.GoToAsync(nameof(LoginCredentialItemPage), true, new Dictionary<string, object>
    {
        ["Item"] = item
    });
}

public async void OnItemAdd(System.Object sender, System.EventArgs e)
{
    await Shell.Current.GoToAsync(nameof(LoginCredentialItemPage), true, new Dictionary<string, object>
    {
        ["Item"] = new LoginCredential()
    });
}

3. 修正后的LoginCredentialListPage的OnNavigatedTo方法

public override async void OnNavigatedTo(NavigatedToEventArgs args)
{
    base.OnNavigatedTo(args);

    var items = await database.GetItemsAsync();

    MainThread.BeginInvokeOnMainThread(() =>
    {
        Items.Clear();
        foreach (var item in items)
            Items.Add(item);
    });
}

五、其他注意事项

  • 确保Constants.DatabasePath已正确定义,指向合法的SQLite数据库文件路径,MAUI中通常可通过FileSystem.AppDataDirectory + "/passwords.db3"获取。
  • 事件处理方法使用async void可能导致未捕获的异常,建议对于非生命周期的事件方法,考虑使用async Task配合ICommand实现,不过当前写法可正常运行。

内容的提问来源于stack exchange,提问作者user19989353

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 09:45:33