Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# iSharedReferences存储的字符串在重新启动我的应用程序后正在更新_C#_Android_Xamarin.android_Visual Studio 2019 - Fatal编程技术网

C# iSharedReferences存储的字符串在重新启动我的应用程序后正在更新

C# iSharedReferences存储的字符串在重新启动我的应用程序后正在更新,c#,android,xamarin.android,visual-studio-2019,C#,Android,Xamarin.android,Visual Studio 2019,我正在从一个活动中传递一个字符串来分割一切,但我传递的字符串不会立即更新,它会在我的应用程序重新启动后更新。通过谷歌搜索,我尝试将我的代码“editor.commit”更改为“editor.apply”,但没有用请帮助我提出建议,谢谢 Activity.cs Fragment.cs 公共类片段1:Android.Support.V4.App.Fragment { 公共字符串mString; 创建时公共覆盖无效(Bundle savedInstanceState) { base.OnCreate(

我正在从一个活动中传递一个字符串来分割一切,但我传递的字符串不会立即更新,它会在我的应用程序重新启动后更新。通过谷歌搜索,我尝试将我的代码“editor.commit”更改为“editor.apply”,但没有用请帮助我提出建议,谢谢

Activity.cs

Fragment.cs

公共类片段1:Android.Support.V4.App.Fragment
{
公共字符串mString;
创建时公共覆盖无效(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
iSharedReferences prefs=PreferenceManager.GetDefaultSharedReferences(Android.App.Application.Context);
mString=prefs.GetString(“数据”,“数据”);
}
创建视图上的公共覆盖视图(布局、充气机、视图组容器、捆绑包保存状态)
{
var视图=充气机。充气(Resource.Layout.Fragment1,容器,false);
var textView=view.findviewbyd(Resource.Id.txtView);
Text=mString;
返回视图;
}
}

如果要刷新片段内容,可以分离片段并重新附着它

这是Fragment1.cs,您只需要使用OnCreateView事件,它将在manager.BeginTransaction()中运行代码时触发。Detach(frg).Attach(frg).Commit()

公共类片段1:片段
{
私家图文电视;
公共字符串mString;
创建视图上的公共覆盖视图(布局、充气机、视图组容器、捆绑包保存状态)
{
//使用此选项可返回此片段的自定义视图
//返回充气器。充气(Resource.Layout.YourFragment,container,false);
iSharedReferences prefs=PreferenceManager.GetDefaultSharedReferences(Android.App.Application.Context);
mString=prefs.GetString(“数据”,“数据”);
视图v=充气机。充气(Resource.Layout.Fragment1,容器,false);
tv=v.findviewbyd(Resource.Id.textView1);
tv.Text=mString;
返回v;
}
}
公共类MainActivity:AppCompativeActivity
{
专用按钮btn1;
私人编辑文本编辑;
私有静态Android.App.FragmentManager;
创建时受保护的覆盖无效(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
//从“主”布局资源设置视图
SetContentView(Resource.Layout.activity_main);
经理=碎片经理;
Fragment1片段=新的Fragment1();
manager.BeginTransaction().Add(Resource.Id.frameLayout1,片段,“myfragmanetag”).Commit();
//t.Add(Resource.Id.fragment1,fragment);
btn1=FindViewById(Resource.Id.button1);
edit=findviewbyd(Resource.Id.editText1);
btn1.单击+=委派
{
var text=edit.text.ToString();
iSharedReferences prefs=PreferenceManager.GetDefaultSharedReferences(此);
iSharedReferencesEditor=prefs.Edit();
编辑器.PutString(“数据”,文本);
editor.Apply();
//Commit();
片段frg=null;
frg=manager.FindFragmentByTag(“myfragmanetag”);
manager.BeginTransaction().Detach(frg.Attach)(frg.Commit();
};
}
}
Main.xaml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<EditText android:id="@+id/editText1" android:layout_width="match_parent" 
        android:layout_height="wrap_content" />
<Button android:id="@+id/button1" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" android:text="pass data" />


<FrameLayout android:id="@+id/frameLayout1" android:layout_width="match_parent" 
        android:layout_height="match_parent" />

    </LinearLayout>

片段1.xaml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:id="@+id/textView1" 
          android:layout_width="match_parent" 
          android:layout_height="wrap_content" 
          android:text="this is test" />
   </LinearLayout>


删除编辑器。清除()@L2_摊铺机我把它弄成了树但没有use@kiran,有什么更新吗?如果我的回复解决了您的问题,请记住将我的回复标记为答案,谢谢。
public class Fragment1 : Fragment
{
    private TextView tv;
    public string mString;

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        // Use this to return your custom view for this Fragment
        // return inflater.Inflate(Resource.Layout.YourFragment, container, false);

        ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(Android.App.Application.Context);
        mString = prefs.GetString("Data", " ");
        View v = inflater.Inflate(Resource.Layout.Fragment1,container,false);
        tv = v.FindViewById<TextView>(Resource.Id.textView1);
        tv.Text =mString;
        return v;
    }
}

public class MainActivity : AppCompatActivity
{
    private Button btn1;
    private EditText edit;          
    private static Android.App.FragmentManager manager;
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.activity_main);


        manager = FragmentManager;
        Fragment1 fragment = new Fragment1();
        manager.BeginTransaction().Add(Resource.Id.frameLayout1, fragment,"myfragmanetag").Commit();

        // t.Add(Resource.Id.fragment1,fragment);

        btn1 = FindViewById<Button>(Resource.Id.button1);
        edit= FindViewById<EditText>(Resource.Id.editText1);
        btn1.Click += delegate
          {
              var text =edit.Text.ToString();
              ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(this);
              ISharedPreferencesEditor editor = prefs.Edit();              
              editor.PutString("Data", text);
              editor.Apply();
              //editor.Commit();
              Fragment frg = null;
              frg = manager.FindFragmentByTag("myfragmanetag");
              manager.BeginTransaction().Detach(frg).Attach(frg).Commit();



          };
    }
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<EditText android:id="@+id/editText1" android:layout_width="match_parent" 
        android:layout_height="wrap_content" />
<Button android:id="@+id/button1" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" android:text="pass data" />


<FrameLayout android:id="@+id/frameLayout1" android:layout_width="match_parent" 
        android:layout_height="match_parent" />

    </LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:id="@+id/textView1" 
          android:layout_width="match_parent" 
          android:layout_height="wrap_content" 
          android:text="this is test" />
   </LinearLayout>