单击Getview中的方法多次调用

时间:2016-07-08 10:00:17

标签: android methods xamarin getview

我想点击方法只调用一次,我该怎么做才能得到这个结果? 这是我的代码:

        public override View GetView(int position, View convertView, ViewGroup parent)
    {
        View result = convertView;
        if (result == null)
        {
            result = LayoutInflater.From(_Context).Inflate(Resource.Layout.DiagnoseTemplateLayout, null, false);
        }
        TextView textView = result.FindViewById<TextView>(Resource.Id.DiagnoseTemplateTextView);
        textView.Text = _Diagnoses[position].Description;
        Button button = result.FindViewById<Button>(Resource.Id.DeleteDiagnoseTemplateButton);
        button.Tag = _Diagnoses[position].Key;
        button.Click += delegate
        {
            if (_Diagnoses[position] != null)
            {
                AlertDialog.Builder builder = new AlertDialog.Builder(_Context);
                builder.SetTitle("Varning");
                builder.SetMessage("Delating selected item?");
                builder.SetPositiveButton("YES", delegate
                {
                    SuperBillAddActivity.SuperBill.Diagnoses.Remove(_Diagnoses[position]);
                    SuperBillAddActivity.RefreshLists();
                });
                builder.SetNegativeButton("NO", delegate { });
                builder.Show();
            }
        };
        return result;
    }

此方法被称为3次按钮。单击+ =委托 从我们点击按钮的位置开始

        public override View GetView(int position, View convertView, ViewGroup parent)
    {
        View result = convertView;
        if (result == null)
        {
            result = LayoutInflater.From(_Context).Inflate(Resource.Layout.DiagnoseTemplateLayout, null, false);
        }
        TextView textView = result.FindViewById<TextView>(Resource.Id.DiagnoseTemplateTextView);
        textView.Text = _Diagnoses[position].Description;
        Button button = result.FindViewById<Button>(Resource.Id.DeleteDiagnoseTemplateButton);
        button.Tag = _Diagnoses[position].Key;
        button.Click += delegate
        {
            if ((_Clicked) && (_Diagnoses[position] != null))
            {
                _Clicked = !_Clicked;
                AlertDialog.Builder builder = new AlertDialog.Builder(_Context);
                builder.SetTitle("Varning");
                builder.SetMessage("Delating selected item?");
                builder.SetPositiveButton("YES", delegate
                {
                    _Clicked = !_Clicked;
                    SuperBillAddActivity.SuperBill.Diagnoses.Remove(_Diagnoses[position]);
                    SuperBillAddActivity.RefreshLists();
                });
                builder.SetNegativeButton("NO", delegate 
                {
                    _Clicked = !_Clicked;
                });
                builder.Show();
            }
        };
        return result;
    }

_Clicked&lt; - 我实现了这个,现在问题解决了,但我不喜欢这种放置代码的方式。有替代品吗? 另外,我很感兴趣为什么第一个代码按照它的方式工作。

1 个答案:

答案 0 :(得分:4)

多次调用

namespace { HRESULT Downlevel_SHCreateItemFromParsingName(PCWSTR pszPath, IBindCtx* pbc, REFIID riid, void** ppv) { _ASSERTE(IsWinVistaOrLater()); HRESULT hResult = HRESULT_FROM_WIN32(ERROR_NOT_SUPPORTED); const HINSTANCE hinstLib = GetModuleHandle(TEXT("shell32")); if (hinstLib) { typedef HRESULT (WINAPI * pfSHCreateItemFromParsingName)(PCWSTR, IBindCtx*, REFIID, void**); const pfSHCreateItemFromParsingName pf = reinterpret_cast<pfSHCreateItemFromParsingName>(GetProcAddress(hinstLib, _CRT_STRINGIZE(SHCreateItemFromParsingName))); if (pf) { hResult = pf(pszPath, pbc, riid, ppv); } } return hResult; } int CALLBACK BrowseForFolderCallbackProc(HWND hWnd, UINT uMsg, LPARAM /* lParam */, LPARAM lData) { if (uMsg == BFFM_INITIALIZED) { // Start with BFFM_SETSELECTION, which is always available. SendMessage(hWnd, BFFM_SETSELECTION, TRUE, lData); #ifdef UNICODE // If possible, also try to use BFFM_SETEXPANDED, which was introduced with // version 6.0 of the shell (Windows XP). SendMessage(hWnd, BFFM_SETEXPANDED, TRUE, lData); // You can also set the caption for the dialog's "OK" button here, if you like // (e.g., by loading a string from a resource). //SendMessage(hWnd, // BFFM_SETOKTEXT, // 0, // reinterpret_cast<LPARAM>(pszOKBtnCaption)); #endif // UNICODE } return 0; } } CString ShowFolderBrowserDialog(HWND hwndOwner, const CString& strDlgTitle, const CString& strStartPath) { if (IsWinVistaOrLater()) { CComPtr<IFileOpenDialog> pFileOpenDlg; if (SUCCEEDED(pFileOpenDlg.CoCreateInstance(__uuidof(FileOpenDialog)))) { if (SUCCEEDED(pFileOpenDlg->SetTitle(strDlgTitle))) { FILEOPENDIALOGOPTIONS options; if (SUCCEEDED(pFileOpenDlg->GetOptions(&options))) { if (SUCCEEDED(pFileOpenDlg->SetOptions(options | FOS_PATHMUSTEXIST | FOS_PICKFOLDERS | FOS_FORCEFILESYSTEM))) { CComPtr<IShellItem> psiStartPath; if (SUCCEEDED(Downlevel_SHCreateItemFromParsingName(static_cast<const TCHAR*>(strStartPath), NULL, IID_PPV_ARGS(&psiStartPath)))) { if (SUCCEEDED(pFileOpenDlg->SetFolder(psiStartPath))) { if (SUCCEEDED(pFileOpenDlg->Show(hwndOwner))) { CComPtr<IShellItem> pShellItemResult; pFileOpenDlg->GetResult(&pShellItemResult); CComHeapPtr<TCHAR> pszSelectedItem; if (SUCCEEDED(pShellItemResult->GetDisplayName(SIGDN_FILESYSPATH, &pszSelectedItem))) { return pszSelectedItem; } } } } } } } } } else { TCHAR szBuffer[MAX_PATH + 1]; szBuffer[0] = TEXT('\0'); BROWSEINFO bi; bi.hwndOwner = hwndOwner; bi.pidlRoot = nullptr; bi.pszDisplayName = szBuffer; bi.lpszTitle = strDlgTitle; bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_NEWDIALOGSTYLE | BIF_SHAREABLE | BIF_NONEWFOLDERBUTTON; bi.lpfn = BrowseForFolderCallbackProc; bi.lParam = reinterpret_cast<LPARAM>(static_cast<const TCHAR*>(strStartPath)); CComHeapPtr<ITEMIDLIST> pidl(SHBrowseForFolder(&bi)); if (pidl && SHGetPathFromIDList(pidl, szBuffer)) { return pszSelectedItem; } } return TEXT(""); } ,因此您的GetView次也是多次。这就是你在button.Click += delegate收到过多电话的原因。

尝试:

Click